Proposal: Dedicated-string-mixin templates/functions

Nick Sabalausky a at a.a
Fri Feb 5 17:40:47 PST 2010


"Chad J" <chadjoan at __spam.is.bad__gmail.com> wrote in message 
news:hkic29$ug7$1 at digitalmars.com...
> Nick Sabalausky wrote:
>> ...
>
> I for one really want something like that.  I think I posted a
> suggestion like this as well:
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=95579
>
> This also came up in another thread and was warmly received, in the
> context of using the syntax on sql queries:
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=102085
>
> I like your syntax too.  Mostly I just want to see this happen somehow.
>
> I also realized such syntax allows us to do this:
>
> int foo = 42;
> string bar = "answer";
>
> writefln("The $bar is $foo."); // Prints "The answer is 42."
>

Great point! Of course, that could probably be done today as:

mixin(writefln("The $bar is $foo."));

but....eeeewww!

> I'd love to be able to do that.
>
> I'd settle for writefln#("The $bar is $foo."); as well if we really need
> to be able to grep mixin usage.  The # could be any token of our choosing.
>

Yea, frankly I'm not certain which way would be better. But personally I'd 
be very happy either way.

However, we should probably take a look around the Nemerle community 
regarding this issue. AIUI, This proposal is essentially how Nemerle's 
mixins work (except they call them macros and have AST features), so it 
could be helpful to see if any of them have found issue with it.

> This all still leaves the problem of parentheses matching.  Parentheses
> make a terrible nesting element.
>
> mixin Foo!
> (`
> someDslCodeHere
> `);
> // Yes, don't forget the terribly asymmetrical semicolon.
>
> versus
>
> mixin Foo!()
> {
> // Valid D tokens only, and {} must be matched.
> someDslCodeHere
> }
>
> I think the latter is worth investing in as well.

D2 has q{}:

mixin Foo!(
q{
    someDslCodeHere
});

But yea, that's is still a good point. Although I think it might be better 
considered part of the more general problem of passing code literals (even 
though it's as delegates in one case and as strings in the other). For 
instance, even without any mixins involved, we still have this kind of mess:

void repeat(int num, 
IForgetTheSyntaxOffhandButLetsJustSayADgThatTakesVoidAndReturnsVoid dg)
{
    for(int i; 0..num)
        dg();
}

// Eeew:
repeat(3,
{
    // Stuff here
});





More information about the Digitalmars-d mailing list