[phobos] expand for std.metastrings

Denis 2korden at gmail.com
Tue Nov 2 10:03:42 PDT 2010


On Tue, Nov 2, 2010 at 6:47 PM, kenji hara <k.hara.pg at gmail.com> wrote:
> I like some short name on clear meaning.
>
> I googled "variable expansion" and "interpolation|interp" with some
> language names.
> There was no significant difference in result.
>
> (I'm not a native English speaker. Please read follows with a grain of salt.)
>
> "interp" may remind us "interpolation(=variable expansion)",
> "interpolation(math)", and "interpreter".
> On the other hand, "expand" is more general word than "interp".
> So, I think that "interp" will be more better .
>
> Kenji
>
> 2010/11/2 Andrei Alexandrescu <andrei at erdani.com>:
>> Thanks, Kenji. All - ideas for a better name? "interp"? Full "interpolate"?
>> Leave it as "expand"?
>>
>> Andrei
>>
>> On 11/2/10 5:40 AM, kenji hara wrote:
>>>
>>> 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
>>>>
>>> _______________________________________________
>>> 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
>>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>

I like expand.

$variable name is good, but it would be more powerful to use
{expression} syntax, e.g. expand!("1 + 2 = {1 + 2}");
I also think that expand could return not a string but rather a tuple
of strings and evaluated expression values.

Currently

int result = 3;
auto expanded = expand!("1 + 2 = ${result}");

evaluates to "`1 + 2 = ` ~ .std.conv.to!string(result) ~ ``". Wouldn't
it be better to evaluate to "tuple(`1 + 2 = `, result)" instead? I
think this would both simplify an "expand", improve performance and
provide additional functionality. expandString could be used get the
current behavior.

Compare: writeln(mixin(expand!("1 + 2 = ${result}"))); vs
writeln(expand!("1 + 2 = ${result}"));

The second one is shorter (doesn't use mixin) and faster (doesn't do
unnecessary concatenations).


More information about the phobos mailing list