I was amazed how well the reflection API worked for building generic Python wrappers of Mojo structs. With the current reflection API, this already works well for types that have Scalar attributes. However, when building conversion methods for attributes such as InlineArray (or now: Array), I hit a wall, because I could not infer the type and size / length parameters without some nasty string parsing.
What would be of great help to me (and IMHO a great addition to the language), would be an addition to the reflection API that allows to infer the parameters of a type. An example for how this could look like:
def convert_from_python[T: ConvertibleFromPython](value: PythonObject) raises -> T:
comptime if reflect[T].base_name == "Array":
# Infer the parameters and call a specialized conversion function
comptime for i in range(reflect[T].param_count):
if reflect[T].param_names[i] == "length":
comptime length = reflect[T].param[i]
comptime for j in range(reflect[T].param_count):
if reflect[T].param_names[i] == "T":
comptime Type = reflect[T].param[j]
return convert_to_array[Array[T, length]](value)
# Now cover other types, too
comptime if reflect[T].base_name == "SIMD":
...
I am certain there would be other good usecases as well.
What do people think about this? Are there alternative ways to achieve this already available?