<div class="gmail_quote">On Wed, Jan 12, 2011 at 10:41 AM, Guilherme Vieira <span dir="ltr"><<a href="mailto:n2.nitrogen@gmail.com">n2.nitrogen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<div><br><div>I'm wondering if a delegate adapter template like isn't handy for Phobos (it may be especially useful for std.signal):</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><div><font face="'courier new', monospace">class Switch</font></div><div><font face="'courier new', monospace">{</font></div><div><font face="'courier new', monospace"> enum State { ON, OFF }</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> void trigger()</font></div><div><font face="'courier new', monospace"> {</font></div>
<div><font face="'courier new', monospace"> switch (mState)</font></div><div><font face="'courier new', monospace"> {</font></div><div><font face="'courier new', monospace"> case State.ON: mState = State.OFF; break;</font></div>
<div><font face="'courier new', monospace"> case State.OFF: mState = State.ON; break;</font></div><div><font face="'courier new', monospace"> default: break;</font></div>
<div><font face="'courier new', monospace"> }</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> if (watch !is null) watch(mState);</font></div>
<div><font face="'courier new', monospace"> }</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> void delegate(State s) watch;</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> private State mState;</font></div><div><font face="'courier new', monospace">}</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">class ToggleButton</font></div><div><font face="'courier new', monospace">{</font></div>
<div><font face="'courier new', monospace"> @property toggled(bool toggled)</font></div><div><font face="'courier new', monospace"> {</font></div><div>
<font face="'courier new', monospace"> writeln("ToggleButton.toggled(", toggled, ")");</font></div><div><font face="'courier new', monospace"> }</font></div>
<div><font face="'courier new', monospace">}</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">void main()</font></div>
<div><font face="'courier new', monospace">{</font></div><div><font face="'courier new', monospace"> scope s = new Switch();</font></div><div><font face="'courier new', monospace"> scope b = new ToggleButton();</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace"> s.watch = &b.toggled; // error: invalid conversion</font></div>
<div><font face="'courier new', monospace"> s.watch = adapt!("obj.toggled = cast(bool)(a)", Switch.State)(b);</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace"> s.trigger(); // prints `</font><span style="font-family:'courier new', monospace">ToggleButton.toggled(true)`</span></div>
<div><font face="'courier new', monospace"> s.trigger();</font><font face="'courier new', monospace"> // prints `</font><span style="font-family:'courier new', monospace">ToggleButton.toggled(false)`</span></div>
<div><font face="'courier new', monospace"> s.trigger();</font><font face="'courier new', monospace"> // prints `</font><span style="font-family:'courier new', monospace">ToggleButton.toggled(true)`</span></div>
<div><font face="'courier new', monospace"> s.trigger();</font><font face="'courier new', monospace"> // prints `</font><span style="font-family:'courier new', monospace">ToggleButton.toggled(false)`</span></div>
<div><font face="'courier new', monospace">}</font></div></div></blockquote><div><br></div><div>Yes, it urges to be polished. Particularly, it doesn't support multiple arguments. I also wanted to place the argument type tuple somwhere else (actually wanted to hide it completely, but I think that's not possible).</div>
<div><br></div><div>Feedback?</div><div><br>-- <br>Atenciosamente / Sincerely,<br>Guilherme ("n2liquid") Vieira<br>
</div></div>
</blockquote></div><div><br></div>Oops, missed the template itself:<div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="'courier new', monospace">template adapt(alias fun, Arg)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">{</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> auto adapt(Object)(Object obj)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> auto adaptImpl = new AdaptImpl!(fun, Object)(obj);</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> return &adaptImpl.opCall!(Arg);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> }</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">class AdaptImpl(alias fun, Object)</font></div><div><font class="Apple-style-span" face="'courier new', monospace">{</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> this(Object obj) { this.obj = obj; }</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> auto opCall(Arg)(Arg a) { return binaryFun!(fun, "a", "obj")(a, obj); }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> private Object obj;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div>
</div></blockquote><div><br>-- <br>Atenciosamente / Sincerely,<br>Guilherme ("n2liquid") Vieira<br>
</div>