[phobos] expand for std.metastrings

Andrei Alexandrescu andrei at erdani.com
Mon Nov 1 15:40:21 PDT 2010


This is great. It's funny I was thinking the same this morning before 
having looked over this message.

As expansion using $xxx is called interpolation in Perl and probably 
other scripting languages I suggest we call the facility "inter".

We can use it with writeln, which I think will be quite popular:

int a = 2;
string b = "hello";
writeln(mixin(inter!"I told you $a times: $str!"));

As shown above, I also suggest that we don't require {} when the 
expression is an identifier.

A debatable language change would allow us to eliminate "mixin", as it 
has been discussed in the newsgroup. A simpler and less dangerous change 
would be to make the paren optional:

writeln(mixin inter!"I told you $a times: $str!");



Andrei

On 10/31/10 10:50 AM, kenji hara wrote:
> I wrote a trivial utility template for generating code string.
> This provides some easiness and viewability of metaprogramming.
> (This is currently used in my adaptTo.)
>
> http://github.com/9rnsr/scrap/blob/master/expand/expand.d
>
> Sample:
> ----
> template GenFunc(string name)
> {
>    mixin(
>      mixin(expand!q{
>        int ${name}(){ return 10; }
>      }));
>    // result of expand! is:
>    //   q{`int ` ~ name ~ `(){ return 10; }`}
> }
> mixin GenFunc!("test");   // generates function "test" returns int
> unittest{
>    assert(test() == 10);
> }
> ----
>
> expand!q{ codes... } parses D code string, and expands ${ expr } for
> embedding expr that is expression evaluated as string in compile-time.
>
> do you think?
>
> Kenji
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list