String mixin syntax sugar

kennytm kennytm at gmail.com
Wed Mar 21 04:35:59 PDT 2012


Mantis <mail.mantis.88 at gmail.com> wrote:
> Hello,
> 
> since people discussed a lot about user-defined attributes recently, I've
> been thinking about a way to implement it with a string mixins. The
> problem with them is their syntax - it's far from what we want to use in
> everyday job. I understand, they should be easily distinguished at use
> site, but perhaps this may be accomplished in other ways as well. My idea
> is to translate this kind of statements:
> 
> # identifier statement
> 

You mean 'declaration'. 

> into this:
> 
> mixin( identifier( q{ statement } ) );
> 
> where an identifier is a, possibly templated, function that accepts one
> string argument and returns a string. Here are some possible use cases:
> 
> #serialize         int a; // marked to be serializable
> #serialize!not int b; // -.- non-serializable
> 
> #readonly float c; // generate trivial private setter and public getter
> 
> #handles!Events.Foo void handler(); // event handler
> 
> #attribute!"Foo" void foo(); // function with additional compile-time info
> 
> Most of these examples require some D parser, but, since it is planned to
> add parser-generation into Phobos, this shouldn't be a problem.
> What do you think, does it have some value for the language, and, if yes,
> is it possible to implement?

The syntax may conflict with '#line'. 

What to do where there are multiple attributes? E.g.

#license!"BSD" @safe #memoize pure nothrow auto invertMatrix(T)(T[]
elements) if (isArithmetic!T) { ... }

What to do with .di files?

// in .di
#handler void onLoad();
// in .d
#handler void onLoad() { .... }

Besides, what's wrong with using '@'?

@serialize int a;


More information about the Digitalmars-d mailing list