Runtime Reflection in Mojo?
Does Mojo support runtime reflection, like in Golang's
reflect
? Or this is on the roadmap>
https://pkg.go.dev/reflect13 Replies
I know there's
Python.type()
but that only works on PythonObjects. I want something of the sort for actual Mojo typesI think decorators are static reflection and code generation, so Mojo may stay that route since there aren't many reasons to have runtime reflection if you can do it all at compile time.
I'd like to have macros like in Julia (or Rust) but I think I read somewhere that this isn't planned.
MLIR-based reflection, potentially with type system access, is both more powerful and less of a giant pain to use. If not for syn and quote Rust would have far less proc macros.
I'd find it helpful to see a whole bunch of examples of MLIR reflection with type system access before accepting a pronouncement about how they fare vs macros. Currently, I have no idea what it's supposed to be. Also, while I like Rust, I wouldn't put it's macro system up as the only macro system to compare with. There's also Nim, Scala, and Typed Racket, amongst others.
Mojo doesn't have a AST in the ordinary sense. It's lowered to some MLIR dialect directly (
lit
?). So even if we were to build a macro system, it will still effectively be MLIR reflection (or simply an IR builder).And, that already exists to a degree in the form of the pdl dialect.
TIL something new!
I think that about 90% of what proc macros do can be handled by static reflection. The remaining 10% is things like sqlx which interact with a database at compile time.
Scala’s macro system, while powerful, was so tightly bound to the compiler that scala 3 got rid of it because it was hurting compiler evolution.
I mean now without custom decorators there's not much we can do in terms of code generation either?
That would be the main complementary feature for reflection, but you can do some things without code generation.
Could you give an example of what we can do for reflection currently?
None of it is public, but @value uses reflection.