[phobos] expand for std.metastrings

kenji hara k.hara.pg at gmail.com
Tue Nov 2 03:40:35 PDT 2010


Thanks for your response, Andrei.

I applied improvements that suggested from you.
- Identifier expression currently does not require braces.
- Interpolation expression is converted to string implicitly (with
std.conv.to!string(...)).

It is still not renamed to 'inter' in github, but I would to do it
when committing to Phobos.

Kenji Hara

2010/11/2 Andrei Alexandrescu <andrei at erdani.com>:
> 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
>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>


More information about the phobos mailing list