4 ms·
Rule 1 of metaprogramming: don't use it when something else will do (almost) as well. Still, I think there is a need. Take declarative DSLs for instance. The p
by norswap 8y ago
Rule 1 of metaprogramming: don't use it when something else will do (almost) as well.
Still, I think there is a need. Take declarative DSLs for instance. The problem with how that's done in all languages right now is that these DSL are running code that build a tree model of sorts.
In some languages like Java or C# that's already ugly because the code must live in an variable initializer or method of a class somewhere (why couldn't it be it's own thing?).
Another problem: how to "refer" to a different part of your model? Solution 1: pass bits of trees around. So you need to store them in variables. In almost all languages this precludes forward references and circular structures. Solution 2: use strings, with all the safety and performance issues that does entail.
Then you have to realize the model is not first-class, it's just a data structure being built. So you can't generate useful methods based on it - you need to have generic functions that take your model as input and interpret it on the fly.
There are many many more use cases, but this one is the one that irks me the most. Why the hell do I need sinful XML, Groovy or what not to compile my Java project. Heck, even make. With some decent (compile-time!) meta-programming support, I could have all this as a DSL with all the might of the language tooling (autocompletion, IDEs etc) to bear.