www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A 'contained' string-pasting ability.

I think the string-pasting capability that people like Don Clugston 
requested in 'Template-based preprocessing'  could be possible without 
too many changes to DMD by restricting its implementation to the alias 
declaration alone. Everywhere else you would have to still give _real_ 
identifiers (ones which can be determined in the lexical, not semantic 
pass) but in alias statements, you could give _virtual_ identifiers 
(ones which rely on something in the semantic pass).

Since you can rename practically everything with alias, it (I think) 
gives you pretty much all the functionality of an identifier keyword 
which can go anyway, but with a much more contained change to the 
language. This way, everything would parse just as before except for 
alias statements, which would allow for delayed evaluation (ie until the 
semantic pass) just like static if does.

AliasDeclaration:
	alias AliasIdent AliasIdent;
	
AliasIdent:
	Identifier
	!( AssignExpression )
	
The AssignExpression must evaluate to a string which is a valid symbol 
name. This would allow solving Garett Bass's problem, for instance (the 
original poster in Template-based preprocessing):

#define SERIALIZABLE(type, identifier) \
    type identifier;                   \
                                       \

        return toString(identifier);   \
    }

class Foo {
    SERIALIZABLE(int, i);
};

could look like:
template Serializable(T, char[] identifier)
{
     private T temp;
     alias temp !(identifier);

     private string serialize_temp() { return toString(identifier); }
     alias serialize_temp !("serialize_" ~ identifier);
}

class Foo {
     mixin Serializable!(int, "i");
}

What do you think?

Reiner
Oct 04 2006