28629, "jabraham17", "[Feature Request]: add 'Reflection.getField' on a type to return the type", "2026-04-01T16:40:59Z"
While implementing improved unions and working closely with the reflection module, I noticed this asymmetry. I can do getField(myObj, index), but not getField(MyType, i). However, the underlying primitive supports this and we have tests that rely on that pattern
For example
proc type deserializeFrom(reader: fileReader(?), ref deserializer) {
type fieldType = __primitive("field by num", this, 0);
return new G(reader.read(fieldType));
}
In this case, this is a type. In order to determine the field type without the primitive, you need a dummy variable
proc type deserializeFrom(reader: fileReader(?), ref deserializer) {
var dummy: this;
type fieldType = Reflection.getField(dummy, 0).type;
return new G(reader.read(fieldType));
}
This feels very unsatisfying.
I could imagine a pair of functions like these, which when given a type just always return the type of the field
proc getField(type MyType, param idx: int) type
proc getField(type MyType, param name: string) type
I could see how it these functions could be confusing, and perhaps the name should be getFieldType. But I do think this is useful functionality to include in the Reflection module.