eval() (Was: One more update on d-programming-language.org)

Nick Sabalausky a at a.a
Thu Sep 16 17:29:40 PDT 2010


"Philippe Sigaud" <philippe.sigaud at gmail.com> wrote in message 
news:mailman.232.1284670065.858.digitalmars-d at puremagic.com...
> On Wed, Sep 15, 2010 at 22:19, Nick Sabalausky <a at a.a> wrote:
>
>> One thing that could be used for that is the eval() function I've 
>> recently
>> added to my SemiTwist D Tools library:
>>
>>
>> http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/process.d
>>
>> Example:
>> -----------------------------
>> import semitwist.util.all;
>> void main()
>> {
>>    auto x = eval!int(q{ return 21 * 2; }); // String can be
>> runtime-generated
>>    assert(x == 42);
>>
>>    eval!void( q{ writeln("Hello World"); }, q{ import std.stdio; } );
>> }
>>
>
> Oh, interesting!
> I had a look at your code. I like this part:
>
> code = boilerplate.format(imports, TRet.stringof, code);
>

Yea, I love format :). It has all sorts of nifty applications. Although, 
what I'd really like is a utility more like this:

"Hello $(name), the answer is $(answer)".populate(["name":"Mr. Prefect", 
"answer":"42"])

Which really wouldn't be hard to do. And maybe beef it up by having it take 
a Variant[string] instead of string[string]. And maybe make the template 
string optionally a template parameter so it can pre-parse it at 
compile-time.

But format's a very nice next-best-thing.

> Inside boilerplate, I wonder if it's possible to test for auto ret = 
> _main()
> inside a static if(is(typeof(  )))? That way, if _main() 'returns' a void,
> the static if won't check and you know you have a void return value.
>

Not sure I get what you mean or what benefit you're going for here...?




More information about the Digitalmars-d mailing list