Idea For Attributes/Annotations

Daniel Keep daniel.keep.lists at gmail.com
Tue Mar 10 20:35:09 PDT 2009


On the syntax for D, a few thoughts:

In Python, the decorator expression must evaluate to a function, which
is then called with the function being decorated.  We could do something
similar in D.

Let's change the syntax to this for now:

@(expr) decl

Let's assume that expr must be a template.  The template will be passed
two arguments, although the second may be omitted.

template foo(alias Decl, char[] src) { ... }

Decl is an alias to whatever is being declared.  This allows functions,
classes, structs, enums, constants, etc. to be decorated.  The second
argument is the source code for that construct.

So let's take an example:

@(foo) void bar();

If foo has a public member called 'foo', then the code is rewritten as:

private void __decorated_bar;
mixin foo!(__decorated_bar, "void bar();");
alias foo!(__decorated_bar, "void bar();").foo bar;

Otherwise, it should be rewritten as:

void bar();
mixin foo!(bar, "void bar();");

This would allow the decorator to, for example, generate a toString
method for an enum without touching the enum itself, wrap a class, or both.

A final idea, this:

@(expr) { decl_a; decl_b; }

decl_c;

@(expr):
  decl_d;
  decl_e;

Could be rewritten as:

@(expr) decl_a;
@(expr) decl_b;
decl_c;
@(expr) decl_d;
@(expr) decl_e;

  -- Daniel



More information about the Digitalmars-d mailing list