Proposal: Dedicated-string-mixin templates/functions

Joel Anderson ask at me.com
Thu Feb 11 08:46:21 PST 2010


On 2/5/2010 3:13 AM, Nick Sabalausky wrote:
> Point #1: It's often been noted that string mixin syntax is ugly. Which is a
> bad thing in and of itself, but it also tends to discourage use of string
> mixins despite their high degree of usefulness.
>
> Point #2: It seems to me that the vast majority of templates and functions
> are either designed specifically to be used as a string mixin or
> specifically designed to be used as something other than a string mixin.
> Only rarely does a single template or function seem to be particularly
> useful both ways.
>
> Proposal:
> So how about taking a cue from Nemerle:
>
> Current:
> -----------------------
> template foo1 {
>      const char[] foo1 = "int a;";
> }
> char[] foo2() {
>      return "int b;";
> }
> mixin(foo1!());
> mixin(foo2());
> -----------------------
>
> Proposed:
> -----------------------
> mixin template foo1 {
>      const char[] foo1 = "int a;";
> }
> mixin char[] foo2() {
>      return "int b;";
> }
> foo1!();
> foo2();
> -----------------------
>
> One consequence of this worth noting is that the current string-mixin could
> be trivially recreated under the proposed syntax:
>
> -----------------------
> mixin char[] mixinString(char[] str) {
>      return str;
> }
> mixinString("int a;");
> -----------------------
>
> Maybe someone not as half-asleep as I currently am can point out a
> clean/clever way to retrieve the string value of such a template/function
> without actually mixing it in, to cover the occasional cases where that
> actually would be useful.
>
>

I like having nicer mixing.  My proposal was to allow templates to take 
a string.  That would make the mixin both visible but still make it look 
neat.

void mixinString(string T)()
{
	mixin(func(T));
}

On the call site...

mixinString!("int a;");

The extra ! indicates that the string must be compile time constant 
because its going into a template.



More information about the Digitalmars-d mailing list