Hello Yuriv,

Currently declarative meta-programming is somewhat special in D compared to dynamic languages like python in a sense that all language entities are actually immutable. You can use those as a base for code generation but can't re-write existing symbols - this is a very basic property of the language and I highly doubt this will be ever changed (I would have been among first protesters, actually).

That means that you need to organize your code differently to achieve similar usability in D. Two main patterns I am aware of:

1)
Apply UDA's in context of some compile-time framework and put any modifications to be done on calling side while preserving actual function / class signatures as-is.

For example, when vibe.d REST interface is annotated with @rootPathFromName, it does not actually change anything in interface but indicates to route generation comile-time algorithm that it should be processed differently. In all other contexts interface still can be used as it has no UDA attached.

2)
Use a private symbol as an input to generate actual public one. Something like this:

@SomeUDA
private class MyClassImpl
{
    // ...
}

// `generate` being some templated CTFE function that used reflection to
// build code of new public class to mixin (with a name "MyClass")
mixin(generate!(MyClassImpl, "MyClass"));

There are several common utilities missing in Phobos to make such coding style much easier but will definitely improve in that regard ;)

One example of function attribute implementation somewhat similar in spirit to Python decorators I have written for vibe.d : https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/internal/meta/funcattr.d

I will also be speaking on this topic on upcoming DConf.

Reply via email to