2 ms·
Functions are "types" just like everything else in rust. And you can implement traits (which can add methods) on any type. So its just "normal" rust code to add
by _cart 6y ago
Functions are "types" just like everything else in rust. And you can implement traits (which can add methods) on any type. So its just "normal" rust code to add a function call to a function of a certain type.
This enables us to (statically) extract information from the type signature of the function and use it to populate the relevant values using the ECS. We also use this type information to automatically (and safely) parallelize system execution based on what ECS resources are accessed (and if they are mutated).
We do (internally) use macros to implement our IntoSystem trait for all relevant function types, but there are no macros in user-facing code.
- linkdd 6y agoI see, that's interesting and it seems powerful. But I would never use such a feature, it increases the code mental load (what you should know before being able to read the code). From one codebase to another, I can't even keep the same assumptions.
- _cart 6y agoYeah implementing traits for functions is rarely advisable. However in this one very specific case (what amounts to dependency injection) it gives us very nice clarity and ergonomics. I'll take that over the macros everyone else is using in this context, or the expensive "runtime reflection" other languages might use to do the same thing. At least its still "normal" static rust code.