M
Modular3mo ago
a2svior

Runtime Reflection in Mojo?

Does Mojo support runtime reflection, like in Golang's reflect ? Or this is on the roadmap> https://pkg.go.dev/reflect
13 Replies
a2svior
a2sviorOP3mo ago
I know there's Python.type() but that only works on PythonObjects. I want something of the sort for actual Mojo types
Darkmatter
Darkmatter3mo ago
I 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.
Peter Homola
Peter Homola3mo ago
I'd like to have macros like in Julia (or Rust) but I think I read somewhere that this isn't planned.
Darkmatter
Darkmatter3mo ago
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.
bpr
bpr3mo ago
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.
sora
sora3mo ago
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).
Darkmatter
Darkmatter3mo ago
And, that already exists to a degree in the form of the pdl dialect.
bpr
bpr3mo ago
TIL something new!
Darkmatter
Darkmatter3mo ago
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.
a2svior
a2sviorOP3mo ago
I mean now without custom decorators there's not much we can do in terms of code generation either?
Darkmatter
Darkmatter3mo ago
That would be the main complementary feature for reflection, but you can do some things without code generation.
a2svior
a2sviorOP3mo ago
Could you give an example of what we can do for reflection currently?
Darkmatter
Darkmatter3mo ago
None of it is public, but @value uses reflection.
Want results from more Discord servers?
Add your server