Modifying user-defined attributes. UDA templates.

Yuriy yuriy.glukhov at gmail.com
Mon Mar 17 02:17:31 PDT 2014


Hello, I'm new to D, so please excuse me if i'm wrong anywhere. 
Ever since i've learned C++ and started practicing templates 
metaprogramming (after reading Andrei's book) i wished there was 
a language to be low-level as C++, productive as if interpreted 
(e.g. Python), and expressive as... well... here comes D! It's 
features are great, and specifically now i want to talk about 
UDA. This feature is what i lacked in C++, but as you know, 
appetite comes with eating, so i think there's still room for 
improvement in UDAs.

The problem is current UDAs can't modify definition they're 
attached to. The solution is somewhere near Python's decorators, 
but of course, using compile-time templates or mixins.

Here's an example:

template MyAttr(string name, T)
{

}

@MyAttr class MyClass
{
  ... class body
}

Which should evaluate to something like:
class __MyClassTmpThatWillBeRemovedInCompileTime
{
  ... class body
}

mixin(MyAttr!("MyClass", 
__MyClassTmpThatWillBeRemovedInCompileTime));

Next, MyAttr can modify the class, rename it, ignore it (in this 
case MyClass will not be available further), define something 
else instead of it.
I could try and modify dmd to support it at least as a prototype, 
but before, i'd like to know whether such feature was already 
considered and rejected. Thanx.


More information about the Digitalmars-d mailing list