<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/6/18 monarch_dodra <span dir="ltr"><<a href="mailto:monarchdodra@gmail.com" target="_blank">monarchdodra@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="im">On Saturday, 8 June 2013 at 15:56:47 UTC, deadalnix wrote:<br>
</div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">

On Saturday, 8 June 2013 at 14:52:23 UTC, monarch_dodra wrote:<br>
<br></div><div class="im">
Sound like a nice idiom. Why is the first set of () needed ? Can we change the grammar to remove them ?<br>
</div></blockquote>
<br>
I've been playing around with this idiom some more, and found some pretty serious limitations:<br>
<br>
# Scoped block:<br>
Using a lambda functions creates a new scope. Having a "true" labeled block, just like static ifs, should not create a new scope. This is relevant if you want to make sure a constructor or destructor is nothrow, for example (destructor could be handled via a simple attribute followed by a colon. eg:<br>

<br>
void foo()<br>
{<br>
    nothrow {<br>
        Sentinel s; //declare sentinel<br>
    }<br>
<br>
    code_goes_here<br>
<br>
    nothrow:<br>
        clean_up_goes_here_code<br>
        destructors_are_nothrow<br>
}<br>
<br>
A lambda would not allow these two constructs.<br></blockquote><div><br></div><div>In D, variable declaration with default construction is always nothrow. </div><div>So, enclosing the declaration of s by nothrow block is unnecessary.</div>
<div><br></div><div>For nothrow destruction, you can add following static assert in foo().</div><div><br></div><div><div>    static assert(__traits(compiles, ()nothrow{ Sentinel s; }), "Sentinel dtor is not nothrow");</div>
</div><div><br></div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

# Purity<br>
Because a lambda needs to access the frame, any function using the lambda trick can't be made pure:<br>
void foo(T)(T a) @safe<br>
{<br>
    (){<br>
        ++a;<br>
    }();<br>
}<br>
Error: pure function 'main.foo' cannot call impure function literal '__lambda1'<br>
<br>
The workaround is to explicitly pass the arguments, *preferably*, shadowing them for clarity:<br>
void foo(int a) pure<br>
{<br>
    (ref int a){<br>
        ++a;<br>
    }(a);<br>
}<br>
This is already *much* less convenient. Imagine if the block needed to access 3, 5 or even more variabes ! Also, if one of those variables is declared as "auto", then you bring in the "ref typeof(a) a" ugliness. Just no.</blockquote>
<div><br></div><div>This is a compiler bug, and I recently fixed it in git master. Explicit argument passing does not need anymore.</div><div>  </div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

# Performance<br>
I haven't actually tested this one, but I'd be inclined to think there is a performance hit for non-release and non-inline builds. inline builds *may* optimize it away, but I'm not sure...<br></blockquote><div>
<br></div><div>Inlining should remove performance penalty. Nobody holds the immediately called lambda, so it should be treated as a 'scope delegate'. For that, we would need to add a section in language spec to support it.<br>
</div><div> </div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

----------------<br>
So my conclusion is that the lambda tric is a partial workaround. We'd need real support for being able to have specific qualification inside bodies.<br></blockquote><div><br></div><div>I don't think so. We can sufficiently use lambda for the "attribute block".</div>
<div><br></div><div>Kenji Hara</div></div></div></div>