No sh*t..?! @__@ That's so cool! But is it smart enough to know the stack frame doesn't need to go to heap in this case? (since it returns a heap object referecing another heap object, i.e. can it untangle `b' from the stack?)<div>
<div><br></div><div>-- <br>Atenciosamente / Sincerely,<br>Guilherme ("n2liquid") Vieira</div><div><br><br><div class="gmail_quote">On Wed, Jan 12, 2011 at 11:49 AM, Dmitry Olshansky <span dir="ltr"><<a href="mailto:dmitry.olsh@gmail.com">dmitry.olsh@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On 12.01.2011 16:07, Guilherme Vieira wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
Ah, I totally missed that. But what if `s' went out of the scope and the scope ended? Wouldn't the scope reference (the one containing `b') be lost and cause memory corruption?<br>
<br>
E.g.:<br>
<br>
    Switch make_switch()<br>
    {<br></div><div class="im">
        auto s = new Switch();<br>
        auto b = new ToggleButton();<br>
<br></div><div class="im">
        s.watch = (Switch.State state) { b.toggled = cast(bool)(state); };<br>
<br>
<br>
        return s;<br>
    }<br>
<br>
<br>
</div></blockquote>
That's the main point of built-in delegates - the compiler detects them and places the enclosing stack frame on heap, so it's all sort of cool magic that just works :)<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
-- <br>
Atenciosamente / Sincerely,<br>
Guilherme ("n2liquid") Vieira<br>
<br></div><div><div></div><div class="h5">
On Wed, Jan 12, 2011 at 10:57 AM, Dmitry Olshansky <<a href="mailto:dmitry.olsh@gmail.com" target="_blank">dmitry.olsh@gmail.com</a> <mailto:<a href="mailto:dmitry.olsh@gmail.com" target="_blank">dmitry.olsh@gmail.com</a>>> wrote:<br>

<br>
    On 12.01.2011 15:41, Guilherme Vieira wrote:<br>
<br>
        Hi,<br>
<br>
        I'm wondering if a delegate adapter template like isn't handy<br>
        for Phobos (it may be especially useful for std.signal):<br>
<br>
           class Switch<br>
           {<br>
               enum State { ON, OFF }<br>
<br>
               void trigger()<br>
               {<br>
                   switch (mState)<br>
                   {<br>
                       case State.ON: mState = State..OFF; break;<br>
                       case State.OFF: mState = State.ON; break;<br>
                       default: break;<br>
                   }<br>
<br>
                   if (watch !is null) watch(mState);<br>
               }<br>
<br>
               void delegate(State s) watch;<br>
<br>
               private State mState;<br>
           }<br>
<br>
           class ToggleButton<br>
           {<br>
               @property toggled(bool toggled)<br>
               {<br>
                   writeln("ToggleButton.toggled(", toggled, ")");<br>
               }<br>
           }<br>
<br>
           void main()<br>
           {<br>
               scope s = new Switch();<br>
               scope b = new ToggleButton();<br>
<br>
               s.watch = &b.toggled; // error: invalid conversion<br>
               s.watch = adapt!("obj.toggled = cast(bool)(a)",<br>
        Switch.State)(b);<br>
<br>
               s.trigger(); // prints `ToggleButton.toggled(true)`<br>
               s.trigger(); // prints `ToggleButton.toggled(false)`<br>
               s.trigger(); // prints `ToggleButton.toggled(true)`<br>
               s.trigger(); // prints `ToggleButton.toggled(false)`<br>
           }<br>
<br>
<br>
        Yes, it urges to be polished. Particularly, it doesn't support<br>
        multiple arguments. I also wanted to place the argument type<br>
        tuple somwhere else (actually wanted to hide it completely,<br>
        but I think that's not possible).<br>
<br>
        Feedback?<br>
<br>
        --         Atenciosamente / Sincerely,<br>
        Guilherme ("n2liquid") Vieira<br>
<br>
    How is it better then built-in language feature? This works just fine:<br>
       void main()<br>
       {<br>
    //they can't be scope  and compiler enforces this (+ scope is<br>
    deprecated)<br>
    //actually, the orignal code is unsafe - what hapens if adapted<br>
    delegate escapes current scope?<br>
           auto s = new Switch();<br>
           auto b = new ToggleButton();<br>
<br>
<br>
           s.watch = (Switch.State a){ b.toggled = cast(bool)a; };<br>
<br>
           s.trigger(); // prints `ToggleButton.toggled(true)`<br>
           s.trigger(); // prints `ToggleButton.toggled(false)`<br>
           s.trigger(); // prints `ToggleButton.toggled(true)`<br>
           s.trigger(); // prints `ToggleButton.toggled(false)`<br>
       }<br>
<br>
    --     Dmitry Olshansky<br>
<br>
</div></div></blockquote>
<br>
<br>
-- <br><font color="#888888">
Dmitry Olshansky<br><br></font></blockquote></div>
</div></div>