Hacker News new | past | comments | ask | show | jobs | submit login

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?




Do you mean like this kind of registration decorator?

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.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: