4 ms·
Well, overriding dunder methods isn't really metaprogramming. It's just method inheritance/override. Is there anything in D similar to decorators? How does th
by dbcurtis 10y ago
Well, overriding dunder methods isn't really metaprogramming. It's just method inheritance/override.
Is there anything in D similar to decorators? How does the registration pattern work in D, for instance?
- gmfawcett 10y agoDo you mean like this kind of registration decorator? https://github.com/rejectedsoftware/vibe.d/blob/master/examples/rest/source/app.d#L232 https://github.com/rejectedsoftware/vibe.d/blob/master/examp... Decorators (in D, 'user defined attributes', or UDA) work differently. It's not a function-composition feature, but more of a tagging feature. You write a class, function, etc., tag it with custom attributes; and then have a separate compile-time function walk over your code, find the decorators, and augment the code based the meaning of the tags. (I used to wish that D had adopted Python-style decorators, since they are easy to reason about and implement, but I can see the logic of the more general UDA system that they adopted.) In practice, though, you would often use templates to achieve the same effect. Given a memoize template (really, just a memoize function), and an expensive-computation function, auto fastComputer = memoize!expensiveComputer; produces roughly the equivalent of @memoize def expensiveComputer(): ... but with opportunities for compile-time optimization.