C# 4.0 / dynamic / operatorsAccording to the &quot;New features in CSharp 4.0&quot; doc, dynamic operator dispatch is supported; is this implemented yet?<div><br></div><div>I was trying to compare to an existing mechanism for using operators with generics (via Expression), but it doesn't compile:</div><div><br></div><div><div>using System;</div><div>using System.Diagnostics;</div><div>using System.Linq.Expressions;</div><div><br></div><div><br></div><div>class Program</div><div>{</div><div>    static void Main(string[] args)</div><div>    {</div><div>        TestOperator&lt;int&gt;(10, 2);</div><div>        TestDynamic&lt;int&gt;(10,2);</div><div>    }</div><div>    const int COUNT = 10000000;</div><div>    static void TestOperator&lt;T&gt;(T seed, T inc)</div><div>    {</div><div>        T value = seed;</div><div>        Stopwatch watch = Stopwatch.StartNew();</div><div>        for (int i = 0; i &lt; COUNT; i++)</div><div>        {</div><div>            value = OperatorCache&lt;T&gt;.Add(value, inc);</div><div>        }</div><div>        watch.Stop();</div><div>        Console.WriteLine(&quot;Operator: {0} ({1})&quot;, watch.ElapsedMilliseconds, value);</div><div>    }</div><div>    static void TestDynamic&lt;T&gt;(T seed, T inc)</div><div>    {</div><div>        dynamic value = seed;</div><div>        //dynamic tmp = inc;</div><div>        Stopwatch watch = Stopwatch.StartNew();</div><div>        for (int i = 0; i &lt; COUNT; i++)</div><div>        {</div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            // Error</font></font><font class=Apple-tab-span style="white-space:pre"><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)"> </font></font></font><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">1</font></font><font class=Apple-tab-span style="white-space:pre"><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)"> </font></font></font><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">Operator '+' cannot be applied to operands of type '::dynamic' and 'T'</font></font></div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            // (note I also tried with dynamic + dynamic, via &quot;tmp&quot;)</font></font></div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            value = value + inc; // tmp</font></font></div><div>        }</div><div>        watch.Stop();</div><div>        Console.WriteLine(&quot;Dynamic: {0} ({1})&quot;, watch.ElapsedMilliseconds, value);</div><div>    }</div><div>    static class OperatorCache&lt;T&gt;</div><div>    {</div><div>        public static readonly Func&lt;T, T, T&gt; Add;</div><div>        static OperatorCache()</div><div>        {</div><div>            var p1 = Expression.Parameter(typeof(T), &quot;x&quot;);</div><div>            var p2 = Expression.Parameter(typeof(T), &quot;y&quot;);</div><div>            var body = Expression.Add(p1, p2);</div><div>            Add = Expression.Lambda&lt;Func&lt;T, T, T&gt;&gt;(body, p1, p2).Compile();</div><div>        }</div><div>    }</div><div>}</div><div><br></div></div><div><br></div><div><br></div><hr size=1 align=left width="25%">Marc [C# MVP]© 2009 Microsoft Corporation. All rights reserved.Thu, 04 Dec 2008 05:13:18 Z287db1b9-c135-40bc-a1c5-a8a51efbfc65http://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#287db1b9-c135-40bc-a1c5-a8a51efbfc65http://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#287db1b9-c135-40bc-a1c5-a8a51efbfc65Marc Gravellhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marc%20GravellC# 4.0 / dynamic / operatorsAccording to the &quot;New features in CSharp 4.0&quot; doc, dynamic operator dispatch is supported; is this implemented yet?<div><br></div><div>I was trying to compare to an existing mechanism for using operators with generics (via Expression), but it doesn't compile:</div><div><br></div><div><div>using System;</div><div>using System.Diagnostics;</div><div>using System.Linq.Expressions;</div><div><br></div><div><br></div><div>class Program</div><div>{</div><div>    static void Main(string[] args)</div><div>    {</div><div>        TestOperator&lt;int&gt;(10, 2);</div><div>        TestDynamic&lt;int&gt;(10,2);</div><div>    }</div><div>    const int COUNT = 10000000;</div><div>    static void TestOperator&lt;T&gt;(T seed, T inc)</div><div>    {</div><div>        T value = seed;</div><div>        Stopwatch watch = Stopwatch.StartNew();</div><div>        for (int i = 0; i &lt; COUNT; i++)</div><div>        {</div><div>            value = OperatorCache&lt;T&gt;.Add(value, inc);</div><div>        }</div><div>        watch.Stop();</div><div>        Console.WriteLine(&quot;Operator: {0} ({1})&quot;, watch.ElapsedMilliseconds, value);</div><div>    }</div><div>    static void TestDynamic&lt;T&gt;(T seed, T inc)</div><div>    {</div><div>        dynamic value = seed;</div><div>        //dynamic tmp = inc;</div><div>        Stopwatch watch = Stopwatch.StartNew();</div><div>        for (int i = 0; i &lt; COUNT; i++)</div><div>        {</div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            // Error</font></font><font class=Apple-tab-span style="white-space:pre"><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)"> </font></font></font><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">1</font></font><font class=Apple-tab-span style="white-space:pre"><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)"> </font></font></font><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">Operator '+' cannot be applied to operands of type '::dynamic' and 'T'</font></font></div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            // (note I also tried with dynamic + dynamic, via &quot;tmp&quot;)</font></font></div><div><font class=Apple-style-span style="background-color:rgb(204, 204, 204)"><font class=Apple-style-span style="color:rgb(255, 0, 0)">            value = value + inc; // tmp</font></font></div><div>        }</div><div>        watch.Stop();</div><div>        Console.WriteLine(&quot;Dynamic: {0} ({1})&quot;, watch.ElapsedMilliseconds, value);</div><div>    }</div><div>    static class OperatorCache&lt;T&gt;</div><div>    {</div><div>        public static readonly Func&lt;T, T, T&gt; Add;</div><div>        static OperatorCache()</div><div>        {</div><div>            var p1 = Expression.Parameter(typeof(T), &quot;x&quot;);</div><div>            var p2 = Expression.Parameter(typeof(T), &quot;y&quot;);</div><div>            var body = Expression.Add(p1, p2);</div><div>            Add = Expression.Lambda&lt;Func&lt;T, T, T&gt;&gt;(body, p1, p2).Compile();</div><div>        }</div><div>    }</div><div>}</div><div><br></div></div><div><br></div><div><br></div><hr size=1 align=left width="25%">Marc [C# MVP]Mon, 03 Nov 2008 16:00:49 Z2008-11-03T16:02:03Zhttp://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#af7f1a06-c936-416e-aed5-e23ad6b9a7c4http://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#af7f1a06-c936-416e-aed5-e23ad6b9a7c4RednaxelaFXhttp://social.msdn.microsoft.com/Profile/en-US/?user=RednaxelaFXC# 4.0 / dynamic / operators Chris Burrow <a href="http://blogs.msdn.com/cburrows/archive/2008/10/29/c-dynamic-part-iii.aspx">mentioned</a>:<br><em>If you're wondering what does work for dynamic C# in the CTP build, here's the short story: (a) dynamic <b>method calls</b>, (b) dynamic <b>property gets/sets</b>, and (c) dynamic <b>conversion</b> sites.<br><br></em>So I guess the answer is dynamic operator dispatch is not yet implemented in the CTP, and so <em>value + inc</em> will fail because it's a UnaryOperationAction (the receiver of method call doesn't count, so it's a UnaryOperationAction instead of a BinaryOperationAction).Tue, 04 Nov 2008 05:53:41 Z2008-11-09T09:16:03Zhttp://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#9df02e66-a23d-4ae4-b978-ae9ce1155889http://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/287db1b9-c135-40bc-a1c5-a8a51efbfc65#9df02e66-a23d-4ae4-b978-ae9ce1155889Marc Gravellhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marc%20GravellC# 4.0 / dynamic / operatorsCheers<hr size="1" align="left" width="25%">Marc [C# MVP]Tue, 04 Nov 2008 07:44:33 Z2008-11-04T07:44:33Z