<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx</link><description>As I mentioned last time , there are a few gotchas that we'll need to look at in order to get a full understanding of the dynamic feature and its capabilities. Today we'll take a look at some of those limitations. As we go along, I'll try to shed some</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Dynamic in C# Part VI: What Dynamic Does not Do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9224398</link><pubDate>Tue, 16 Dec 2008 10:08:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9224398</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9224534</link><pubDate>Tue, 16 Dec 2008 10:41:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9224534</guid><dc:creator>David M. Kean</dc:creator><description>&lt;p&gt;The second example is wrong - I think you mean:&lt;/p&gt;
&lt;p&gt;dynamic d = new D();&lt;/p&gt;
&lt;p&gt;(By-the-way, anonymous comments are disabled)&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9224779</link><pubDate>Tue, 16 Dec 2008 11:44:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9224779</guid><dc:creator>samng</dc:creator><description>&lt;p&gt;Whoops, yes you're right, that was supposed to be dynamic.&lt;/p&gt;
&lt;p&gt;Anonymous comments are disabled by design.. :)&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9225230</link><pubDate>Tue, 16 Dec 2008 14:15:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9225230</guid><dc:creator>int19h</dc:creator><description>&lt;p&gt;&amp;gt; Because of the way the compiler implements explicitly implemented interfaces, C.M gets its name removed (making it uncallable via a C pointer).&lt;/p&gt;
&lt;p&gt;Actually, as I understand, it's not really removed - it's still there exactly as spelled, &amp;quot;C.M&amp;quot; - it's just that it's not a valid C# identifier.&lt;/p&gt;
&lt;p&gt;That said, it would be useful if we could use arbitrary strings for identifiers in dynamic dispatch (and maybe elsewhere). I.e.:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; foo.&amp;quot;&amp;lt;bar&amp;gt;&amp;quot;()&lt;/p&gt;
&lt;p&gt;or something along these lines.&lt;/p&gt;
&lt;p&gt;Of course, this still won't help for explicit interface implementations, as those are also private.&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9227377</link><pubDate>Tue, 16 Dec 2008 21:45:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9227377</guid><dc:creator>samng</dc:creator><description>&lt;p&gt;Hey int19h,&lt;/p&gt;
&lt;p&gt;I kind of agree with you, though the current thought is that when you've got a statically known receiver, you can guarantee that your arbitrary string will fail at runtime, since your receiver doesn't implement IDynamicObject.&lt;/p&gt;
&lt;p&gt;We're currently still in the trenches about the question of how much static time information we should use up front, and what we should delay load. For instance, should we check the arity of the methods? Should we check names? Accessibility? Should we do type inference where possible? How far should we go when we have a dynamic argument, since we know that most likely the call will be dispatched dynamically?&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9230710</link><pubDate>Wed, 17 Dec 2008 14:44:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9230710</guid><dc:creator>rgoerss</dc:creator><description>&lt;p&gt;Sam,&lt;/p&gt;
&lt;p&gt;What a great entry this is on the current by-design limitations of the DLR; much thanks for sharing your insight.&lt;/p&gt;
&lt;p&gt;On the matter of accessibility, I think that it would be valuable to make dynamic code behave in the same way as regular C# code wherever possible, particularly with respect to accessibility and type inference which both lend themselves to intuitive coding.&lt;/p&gt;
&lt;p&gt;I think that moving forward it would be preferable to avoid things like....&lt;/p&gt;
&lt;p&gt;IEnumerable&amp;lt;object&amp;gt; someObjects = enumerableInstances; // Inexplicably fail for an entire major version of the .NET Framework.&lt;/p&gt;
&lt;p&gt;Consider this, for example: &amp;nbsp;A self-contained factory model class has a public, static method that consumes dynamic objects returned by a COM server, instantiates a concrete .NET type and returns a public interface that the .NET type implements. &amp;nbsp;In this scenario, which happens to be something I encounter every day when working with legacy machine control systems, it makes perfect sense for a public, static member to hand off a dynamic object to an instance of itself through a private member or constructor.&lt;/p&gt;
&lt;p&gt;Could this scenario be reworked to be compatible with a DLR implementation that does not allow for this type of accessibility with dynamic arguments? &amp;nbsp;Sure, it could. &amp;nbsp;But that would place more of the unboxing and handling of the dynamic instance in the static portion of the type, ostensibly increasing the amount of time spent in a locked/critical state.&lt;/p&gt;
&lt;p&gt;Well, at least this is my take on it, but you are the low level CLR expert -- not me. &amp;nbsp;Maybe my concerns/wants are misguided in some way.&lt;/p&gt;
&lt;p&gt;Thanks again for the great articles, Sam.&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9243186</link><pubDate>Sat, 20 Dec 2008 04:17:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9243186</guid><dc:creator>Gregory Kornblum</dc:creator><description>&lt;p&gt;I am glad that this article tries to somewhat dispell the thoughts of dynamically typed languages such as P *cough* H *cough* P. I was getting a little worried. First MVC then dynamic, I felt as if we were going back 10 years in time. I'd be perfectly OK with that however I am still in my 30s! Seriously though, thanks for pointing out that dynamic is not much more then an evolved object. Keep up the great work over there!&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9249768</link><pubDate>Tue, 23 Dec 2008 14:18:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9249768</guid><dc:creator>Pop.Catalin</dc:creator><description>&lt;p&gt;Please don't introduce inconsistencies in the language. This will make converting from dynamic to static very cumbersome and error phrone. Making use of dynamic to prototype then switch to static types a non valid scenario.&lt;/p&gt;
&lt;p&gt;dynamic foo = 1;&lt;/p&gt;
&lt;p&gt;foo += ++foo + foo++;&lt;/p&gt;
&lt;p&gt;should be evaluated using the same rules the compiler uses to evaluate the next expression with static types (left to right one time evaluation of operands):&lt;/p&gt;
&lt;p&gt;int bar = 1;&lt;/p&gt;
&lt;p&gt;bar += ++bar + bar++;&lt;/p&gt;
&lt;p&gt;Even if it means the CLR team steeping in and adjusting the way the CLR works with boxed primitives (Just like they did with Nullable&amp;lt;T&amp;gt;, and that was a 12th hour change).&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9258888</link><pubDate>Wed, 31 Dec 2008 23:29:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9258888</guid><dc:creator>EdgarHarris</dc:creator><description>&lt;p&gt;One of the things that kills me about C++ is the language is incredibly complex. &amp;nbsp;Paraphrasing Scott Myers, there's an exception to every rule, and an exception to most exceptions. &amp;nbsp;While I think supporting inetrop with dynamic languages is an important feature, some of the behaviors you bring up here really concern me. &amp;nbsp;I think that the issue with struct mutation and accessibility really need to be addressed. &amp;nbsp;If these issues are not addressed we could end up with C# evolving into a language that is as complex or nearly as complex as C++.&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9266994</link><pubDate>Fri, 02 Jan 2009 06:45:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9266994</guid><dc:creator>samng</dc:creator><description>&lt;p&gt;Whew, sorry I haven't responded to comments in a while! Been on Christmas vacation :)&lt;/p&gt;
&lt;p&gt;Okay, lets try to address these:&lt;/p&gt;
&lt;p&gt;Rob - Thanks for your kind words! These comments definitely help make it much easier to keep blogging. I appreciate your thoughts!&lt;/p&gt;
&lt;p&gt;I entirely agree with you - we've recently taken the design change to make this type of scenario work. Dynamic will now consider the same accessibility rules that the static language considers. This means that each of the callsites will encode their calling context (ie the containing type that the callsite lives in) so that the runtime binder can check the accessibility from the calling context location.&lt;/p&gt;
&lt;p&gt;This will then enable the scenario you described, and will give runtime binding errors if the method is not accessible from the calling context location.&lt;/p&gt;
&lt;p&gt;Kornblum - thanks for your comments! We are definitely trying very hard to make sure that the designs that we come up with have been vetted with the community, and are trying to take all the feedback that we can!&lt;/p&gt;
&lt;p&gt;Pop.Catalin - the scenario you described works identically in both dynamic and static code. The thing that does NOT work is the nested struct scenario. This means that you cannot prototype with dynamic and then convert back to struct - this makes sense, since dynamic is a reference type and structs are value types. &lt;/p&gt;
&lt;p&gt;Edgar - as I mentioned above, we're addressing the accessibility issue. I entirely agree with you that these types of differences are bad, and so we're trying to work out all the kinks in the implementation. The nested struct problem is one that we're trying to figure out the philosophy on - since dynamic is a reference type, does it make sense to mutate structs? Combine that with costs and benefits, and you've got the factors that we're considering when making our design decisions.&lt;/p&gt;
&lt;p&gt;Thanks for all the great comments guys!&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9277597</link><pubDate>Mon, 05 Jan 2009 09:54:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9277597</guid><dc:creator>navid_baverd</dc:creator><description>&lt;p&gt;please examples of class in c# FORWARD in id &amp;quot;Baverd_mohammad@hotmail.com&amp;quot;&lt;/p&gt;
&lt;p&gt;Thank you FAR&lt;/p&gt;
</description></item><item><title>Community Convergence XLVIII</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9318681</link><pubDate>Wed, 14 Jan 2009 16:25:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318681</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Welcome to the 48th Community Convergence. The C# team continues to work hard to get out the next version&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9319056</link><pubDate>Wed, 14 Jan 2009 20:22:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319056</guid><dc:creator>commongenius</dc:creator><description>&lt;p&gt;&amp;quot;There is a restriction in the CLR that prevents the compiler from generating non-virtual calls on virtual methods.&amp;quot;&lt;/p&gt;
&lt;p&gt;This is obviously not true, since &amp;quot;base.ToString()&amp;quot; in C# generates &amp;quot;call instance string [mscorlib]System.Object::ToString()&amp;quot;, which is a non-virtual call to a virtual method. Can you clarify what you mean by this?&lt;/p&gt;
&lt;p&gt;I would very strongly agree with the previous posters that EVERY effort needs to be made to preserve intuitive behavior in dynamic dispatch, even to the point of limiting the feature if it is necessary to maintain that intuitive behavior. As Edgar said, the last thing we need is for C# to become C++.&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9319097</link><pubDate>Wed, 14 Jan 2009 20:45:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319097</guid><dc:creator>commongenius</dc:creator><description>&lt;p&gt;Regarding explicit interface implementations, listing out the members of System.Int32 shows members named System.IConvertible.ToBoolean, System.IConvertible.ToChar, etc. I am not clear on what you mean when you say &amp;quot;Because of the way the compiler implements explicitly implemented interfaces, C.M gets its name removed.&amp;quot; It seems to me that the only thing preventing an explicitly implemented interface member is the fact that it is private, and I have to believe that there is a solution to that problem (although I haven't thought of it yet).&lt;/p&gt;
</description></item><item><title>re: Dynamic in C# VI: What dynamic does NOT do</title><link>http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx#9394850</link><pubDate>Wed, 04 Feb 2009 09:56:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9394850</guid><dc:creator>samng</dc:creator><description>&lt;p&gt;Commongenius:&lt;/p&gt;
&lt;p&gt;Great catch! I should clarify that. What I really ought to have said is that the CLR prevents the compiler from generating non-virtual calls on virtual methods from outside the class. Since the runtime binder lives outside of the class that the user is calling a method on, and the delegate that it compiles lives outside the type, we are not allowed to do this.&lt;/p&gt;
&lt;p&gt;The same restriction actually holds true for Expression Trees. We had to put in a workaround for the expression tree () =&amp;gt; base.Foo() by essentially generating a stub method on the current type that does the base call for you, and then generating the call to that stub method from the expression tree.&lt;/p&gt;
&lt;p&gt;This technique works for expression trees, and COULD work for dynamic as well, but since the overload resolution happens at runtime, we would essentially need to generate a stub method for each overload that this occurs in. We therefore decided that this was a fine limitation to live with. However, if we're wrong and people actually feel the need to use this more than we anticipate, we will definitely take a look at the design again in the future!&lt;/p&gt;
&lt;p&gt;Re: your second comment:&lt;/p&gt;
&lt;p&gt;Again, great catch! I should be more clear when I write... :) You're right that the name doesn't get wiped clean - it gets fully qualified. This means that there is no method named &amp;quot;M&amp;quot; on C, but rather there is a &amp;quot;IFoo.M&amp;quot; on it. &lt;/p&gt;
&lt;p&gt;There are two major reasons that require us to keep this limitation though. The first is that explicit interface implementations were meant to be views on the object through an interface view. But at runtime, there are no interfaces, and hence no views. The second (and in my opinion more compelling) reason is that a class may implement several interfaces that have the same overload, and can explicitly implement all of those methods. At that point, which method should we be picking? They all have the same signature, and all have the same name (well, they would be IFoo.M, IBar.M etc). Without the interfaces, there is no way to differentiate between them!&lt;/p&gt;
</description></item></channel></rss>