Is the world coming to an end?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 4 05:41:56 PDT 2011


On Sun, 03 Apr 2011 15:40:18 -0400, Nick Sabalausky <a at a.a> wrote:

> "Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote in message
> news:mailman.3093.1301858962.4748.digitalmars-d at puremagic.com...
>> On 4/3/11, Nick Sabalausky <a at a.a> wrote:
>>> I've always thought that we should be able to do something like this:
>>>
>>> template foo(int val)
>>> {
>>>     enum foo = val.meta.argString;
>>> }
>>> static assert(foo!(2+3) == "2+3");
>>>
>>
>> I wish we had some introspection to get the lines of code inside of a
>> function. But it's still pretty cool that you can pass expressions to
>> functions and automatically construct delegates. See here:
>>
>> import std.stdio;
>>
>> void main()
>> {
>>    int x;
>>
>>    foo(false, x = 5);
>>    assert(x != 5);
>>
>>    foo(true, x = 5);
>>    assert(x == 5);
>> }
>>
>> void foo(bool doit, int delegate()[] dgs ...)
>> {
>>    if (doit)
>>        dgs[0]();
>> }
>>
>> I wonder how many people know about this feature. I just bumped into
>> it a few days ago while looking through the docs.
>
> I didn't know that. I think I remember the idea of it being discussed,  
> but I
> didn't know it was in.

It's been there forever (at least since I first learned D).  It's even in  
D1:

http://www.digitalmars.com/d/1.0/function.html

Look for "Lazy variadic functions"

I think what you were thinking of is the replacement of lazy with simply a  
delegate (which this example is not).

-Steve


More information about the Digitalmars-d mailing list