Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I did not know that Rust had C#-style attributes (annotations)! Will they allow creating custom ones and reflecting on them at runtime?


Like type information, attributes are generally boiled away during the build in order to save space in the executable. So you can't reflect on them at runtime.

However, you can make custom attributes expand to arbitrary generated code via the macro system, allowing something similar. This is how we do fast JSON serialization, for instance: the #[deriving(Encodable)] attribute expands to optimized, type-specialized code at compile time, avoiding costly reflection at runtime. (Note that this is an experimental feature, so it may change post 1.0.)


Reflection is not possible in Rust?


It is somewhat supported at the moment, but it's experimental and we may remove the support for it. Reflection always carries an overhead in terms of binary size and as a result many of us feel that it's inappropriate for a low-level systems language like Rust.


In theory, given the attribute, and macro system, it should be possible to implement this as a library in the language, correct? I think that is vastly preferred over a constant runtime always hanging around.


Runtime reflection is rarely a good idea—it’s just that most languages don’t support any forms of compile-time reflection. Rust does have those tools, and has the power to replace the vast majority of cases where runtime reflection is used with compile-time behaviour.


If you are unaware, the base-language of Rust lacks a runtime. This limits what can easily be done in terms of reflection, when compared to languages like C# and Java.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: