[phobos] expand for std.metastrings

kenji hara k.hara.pg at gmail.com
Tue Nov 2 13:16:30 PDT 2010


And, I do not recommend following use of expand:
writeline(mixin(expand!"take $n times."));

The probrem is:
- expand concatenates string by ~ operators, it is may cost than using
appender(text, formatWrite, writefln...)

I will remove ddoc example from github code.

Kenji

2010/11/3 kenji hara <k.hara.pg at gmail.com>:
> The points of text vs expand are:
> - text is comma separated formatting, expand is inlined formatting.
> - text is general formatting function, expand is mostly useful in
> compile-time, and specialized D code formatting for code generation.
> - editor hilighting issue - my editor does not color q{}, so
> generating code are colored with normal syntax hilighting. If I use
> text and its formatting, code readability will be wrong. I don't like
> it.
>
>> Also, if I
>> recall correctly the plan is to get a large portion of both phobos and D
>> CTFE compatible, which would eventually eliminate the need for the current
>> metastrings.
> Almost agreed. I think std.metastrings.toStringNow should be
> deprecated, and replaced std.conv.to.
> The purpose of std.metastrings I think is that has useful utilities
> often specialized for code generation.
> Expand is just right.
>
> Kenji
>
> 2010/11/3 Robert Jacques <sandford at jhu.edu>:
>> On Tue, 02 Nov 2010 14:27:28 -0400, kenji hara <k.hara.pg at gmail.com> wrote:
>>
>>>> enum code = text(q{ enum msg = "I call you "}, count, q{" times"} );
>>>> enum code = text(` enum msg = "I call you `, count, ` times;` );
>>>> enum code = ` enum msg = "I call you ` ~ count ~ ` times;`
>>>> enum code = q{ enum msg = "I call you "} ~ count ~ q{" times"; }
>>>
>>> 1st and 4th does not generate intended code string, but generates invalid
>>> one.
>>> expand! generates 3rd code automatically cooperation with mixin.
>>> (2nd is ... the code currently others want to generate for run-time
>>> performance?)
>>
>> Opps. Sorry, I tested 2 & 3, but forgot to test 1 & 2. I also seemed to have
>> forgotten a " after times in 2 & 3.
>>
>> Also, what do you mean by: (2nd is ... the code currently others want to
>> generate for run-time performance?)
>>
>>>> I admit ${n} is slightly better than },n,q{, but it's by no means ugly.
>>>> (And
>>>> using `` instead of q{} makes it better: `,n,`)
>>>
>>> The advantage of expand! is that escapes string quotations and
>>> concatenates correctly while keeping readability of our meta-code
>>> well.
>>> (In above 3rd, you escape q{} to `` and interpolate "count" *manually*.)
>>
>> Yes, manually escaping and concatenating, in all but the trivial cases, is
>> really annoying. But my main point was that text is both CTFE-able and lets
>> you cleanly do everything expand does naturally. Expand is more complex to
>> use, isn't self-documenting (i.e. you have to remember to use a mixin), is
>> mostly redundant and increases the radius of comprehension of phobos.
>> However, it does allow you to escape a token strings inside a nested string
>> in a nice in-lined way. So far, the examples don't seem to provide a strong
>> argument for yet another text formater beyond text and Format. Also, if I
>> recall correctly the plan is to get a large portion of both phobos and D
>> CTFE compatible, which would eventually eliminate the need for the current
>> metastrings.
>>
>>
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>>
>


More information about the phobos mailing list