Hello,
I’m currently working on Crystal bindings for AI libraries like Llama.cpp using Vibe Coding.
While doing this, I started thinking about how to build a matrix or tensor library in a statically typed language like Crystal. I began to feel that Variadic Generics might play an important role.
I came across this idea through PEP 646 in Python and a discussion on Hacker News, which show how array shapes (or dimensions) can be expressed and checked at the type level during compilation.
For example, it would be helpful if Crystal could support type signatures like this:
def dot(a : Tensor(Float64, *X, Y), b : Tensor(Float64, Y, *Z)) : Tensor(Float64, *X, *Z)
This kind of typing could help catch shape mismatches at compile time in tensor operations.
There is already an open issue related to this: #7101, but it doesn’t seem to be getting much attention at the moment.
I’m not sure if this idea is really the best approach. In fact, combining static and dynamic typing—like in Python’s NumPy or Ruby’s NArray—might be a more practical solution in many cases, rather than introducing more complexity into the compiler.
Still, I wanted to share this in case it’s useful, and I’d really appreciate hearing thoughts from others who are more experienced or interested in this area. Thank you.
(Translation from Japanese by ChatGPT)