<?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>Calling static methods on type parameters is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx</link><description>There were lots of good comments on my previous entries in this series. I want to address some of them, but first I want to wrap this up by considering how a small change to the scenario makes it plausible to choose a different option. Consider now the</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#5131750</link><pubDate>Wed, 26 Sep 2007 03:12:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5131750</guid><dc:creator>Tim Watson</dc:creator><description>&lt;p&gt;An improved reflection api? Let's face it, nobody has really done this more clearly than in smalltalk, so perhaps the following (deliciously Ruby-ish) would suffice:&lt;/p&gt;
&lt;p&gt;var subject = GetSomeObjectOrOther();&lt;/p&gt;
&lt;p&gt;var args = { &amp;quot;firstArgument&amp;quot;, &amp;quot;secondArgument&amp;quot; };&lt;/p&gt;
&lt;p&gt;operationName = &amp;quot;Foo&amp;quot;;&lt;/p&gt;
&lt;p&gt;var result = subject.Send( operationName, args );&lt;/p&gt;
&lt;p&gt;Although the notion of 'sending messages' is probably a bit alien to some community members; perhaps this would work better instead?&lt;/p&gt;
&lt;p&gt;subject.Dispatch( operationName, args );&lt;/p&gt;
&lt;p&gt;The second parameter can be a param-array of type System.Object. I wonder about methods with void return types though: can there be one signature that fits all? Or will we start seeing stuff that looks reminiscent of VB (gasp!) such as &lt;/p&gt;
&lt;p&gt;subject.Dispatch( &amp;quot;CreateAccount&amp;quot;, accountNumber ); //void return type&lt;/p&gt;
&lt;p&gt;var balance = subject.Call( &amp;quot;GetAccountBalance&amp;quot;, accountNumber ); &lt;/p&gt;
&lt;p&gt;As for property access, what about something pythonic, such as:&lt;/p&gt;
&lt;p&gt;var name = obj.GetAttribute( &amp;quot;Name&amp;quot; );&lt;/p&gt;
&lt;p&gt;obj.SetAttribute( &amp;quot;Name&amp;quot;, &amp;quot;new_&amp;quot; + name );&lt;/p&gt;
&lt;p&gt;Either way, having new methods on System.Object seems better than messing around with the syntax, or am I missing something here!? Also, this would work with *static* methods (because System.Type extends System.Object and is [effectively] the metaclass of a class).&lt;/p&gt;
&lt;p&gt;But this kind of thing can go too far...&lt;/p&gt;
&lt;p&gt;class MyType : System.Type { ... } &amp;nbsp;// s c a r y ! ! ,,, -_- ,,, &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5131750" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#4951058</link><pubDate>Mon, 17 Sep 2007 08:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4951058</guid><dc:creator>David White</dc:creator><description>&lt;p&gt;Here's a potential use case for access to the AST (abstract syntax tree). We'd like to be able completely parse the source code, including pre-processing directives, to be able to intelligently merge source code changes with regenerated code, even if the user re-orders or renames classes, methods, etc. Current parsers (or at least the ones we've tested to date) struggle with pre-processing directives.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4951058" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#4874741</link><pubDate>Wed, 12 Sep 2007 07:03:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4874741</guid><dc:creator>JohnMcD</dc:creator><description>&lt;p&gt;I came across this blog when I was trying to find out why C# didn’t allow virtual static methods or virtual constructors. I’m used to using virtual constructors, virtual class methods and class references in Delphi and I’ve been really frustrated by their absence in C#.&lt;/p&gt;
&lt;p&gt;This blog has clarified the difference between static methods and class methods.&lt;/p&gt;
&lt;p&gt;You said earlier in your blog “[people ask me] why C# does not support “virtual static” methods. I am always at a loss to understand what they could possibly mean, since “virtual” and “static” are opposites! “virtual” means “determine the method to be called based on run time type information”, and “static” means “determine the method to be called solely based on compile time static analysis”.&lt;/p&gt;
&lt;p&gt;I would like to argue the benefits of virtual class methods:&lt;/p&gt;
&lt;p&gt;When a base class has a virtual constructor and various virtual class methods, each subclass can implement its own versions of the constructor and class methods.&lt;/p&gt;
&lt;p&gt;It’s largely class references that make virtual class methods and virtual constructors so useful, but they would also be very useful with generics. (Generics are not implemented in the current version of Delphi).&lt;/p&gt;
&lt;p&gt;Class references in Delphi are a little bit like the Type class in C#, but they can be restricted to just the types that derive from a particular base class, and you can call any class methods or constructors that have been defined in that base class from the class reference. When the class methods are virtual you get the version appropriate for the class reference.&lt;/p&gt;
&lt;p&gt;(When you’re used to this functionality, you wonder why anyone would need to use factory patterns. I like the comment about most design patterns being necessary when the language has failed to provide a needed feature natively).&lt;/p&gt;
&lt;p&gt;For example, virtual class methods could be used to return a description of the class, to determine whether the class could do some required action, or ....&lt;/p&gt;
&lt;p&gt;These classes can all be registered with some manager object, which can then use the class methods to determine which class is appropriate for some action, then create an instance of that class.&lt;/p&gt;
&lt;p&gt;In a C# like syntax (and using static with a slightly changed meaning), it would look like:&lt;/p&gt;
&lt;p&gt;public abstract class Animal&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public abstract static Animal Create();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public abstract static bool CanFly();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public abstract static string Description();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public abstract static int LegCount();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class Eagle: Animal&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public override static Animal Create()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new Eagle();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public override static bool CanFly()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public override static string Description();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return “Large raptor”;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public override static int LegCount();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 2;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;unknown syntax declaring AnimalClass = class of Animal;&lt;/p&gt;
&lt;p&gt;private List&amp;lt;AnimalClass&amp;gt; animalClassList;&lt;/p&gt;
&lt;p&gt;animalClassList.Add(Eagle);&lt;/p&gt;
&lt;p&gt;foreach (AnimalClass animalClass in animalClassList)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if ( animalClass.CanFly() )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;aviary.Add(animalClass.Create());&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This sort of thing is very simple to do in Delphi, and requires much more convoluted code in C#.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4874741" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#3468652</link><pubDate>Sat, 23 Jun 2007 01:19:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3468652</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Stuart: &amp;nbsp;That's certainly a possibility, though I find obj.&amp;quot;Method&amp;quot;() weird. &amp;nbsp;If you know the name already then why are you doing it dynamically? &amp;nbsp;&lt;/p&gt;
&lt;p&gt;In your examples, either its a string literal or a parenthesized expression. The string literal scenario is I think sufficiently implausible that we can not optimize for it -- just put the string literal into the parens if you really want to do that for some strange reason. Then we have only parenthesized expressions to worry about.&lt;/p&gt;
&lt;p&gt;This would then have the nice property that you could do&lt;/p&gt;
&lt;p&gt;obj.(blah)(whatever)&lt;/p&gt;
&lt;p&gt;for any expression blah of type string. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Having parentheses, or some other token to call out dynamism, is probably necessary if we want the semantics to be both unambiguous and not brittle. &amp;nbsp;We do not want to get into this unfortunate situation:&lt;/p&gt;
&lt;p&gt;string Method2 = &amp;quot;Method1&amp;quot;;&lt;/p&gt;
&lt;p&gt;obj.Method2(); // If I then add Method2 to my object, does this call Method1 or Method2? brittle!&lt;/p&gt;
&lt;p&gt;obj.(Method2)(); // unambiguously calls Method1.&lt;/p&gt;
&lt;p&gt;This also has the nice property of not being legal syntax now, since the thing that follows the dot must be an identifier.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3468652" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#3454918</link><pubDate>Fri, 22 Jun 2007 10:56:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3454918</guid><dc:creator>Phil Parker</dc:creator><description>&lt;p&gt;Here's another one which I've been mulling over ... nowhere near a use case, or for that matter even really a complete thought, but I've been wondering about the possibility of enforcing immutability contracts over types.&lt;/p&gt;
&lt;p&gt;My personal preference is to keep mutability to an absolute minimum, for the obvious simplifications it brings to concurrency and avoidance of client mutation of shared state exposed by APIs.&lt;/p&gt;
&lt;p&gt;However, in some cases, there are patterns, usually dealing with extensibility where I would like to be able to enforce immutability. &amp;nbsp;Either as a constraint for internal developers or possibly third party developers.&lt;/p&gt;
&lt;p&gt;Not sure if this would really play with what you're indicating as far as exposing compiler internals. &amp;nbsp;So far, I'd been thinking of it as more likely to be a reflection and decompilation activity at runtime, which would be far too hairy for me to attempt (I have a real job!) and of course would at best only deal with managed code.&lt;/p&gt;
&lt;p&gt;Sorry this isn't really a use case. &amp;nbsp;Just some musings.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3454918" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#3451457</link><pubDate>Fri, 22 Jun 2007 05:19:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3451457</guid><dc:creator>Max Bolingbroke</dc:creator><description>&lt;p&gt;Access to C# compiler internals would really help me accomplish what I'm trying to do. Basically, I have a working compile time metaprogramming system for C#, which postprocesses an assembly to e.g. add code to check for nulls for method parameters marked with a [Required] attribute. This is limited however, because it can only implement transforms that do not change the interface of the class in any meaningful way: this means that I cannot, for example, create a [Property] macro which could be attached to a class field and spits out the getter/setter at compile-time for you.&lt;/p&gt;
&lt;p&gt;However, I'm not sure if you guys are great fans of metaprogramming: it would certainly be a bit &amp;quot;out there&amp;quot; compared to the features C# has at the moment. And people can always use Boo, especially with the new meta methods feature (&lt;a rel="nofollow" target="_new" href="http://blogs.codehaus.org/people/bamboo/archives/001593_boo_meta_methods.html"&gt;http://blogs.codehaus.org/people/bamboo/archives/001593_boo_meta_methods.html&lt;/a&gt;).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3451457" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#3450190</link><pubDate>Fri, 22 Jun 2007 03:01:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3450190</guid><dc:creator>Stuart Ballard</dc:creator><description>&lt;p&gt;My thought on dynamic method/property access:&lt;/p&gt;
&lt;p&gt;obj.&amp;quot;Method&amp;quot;();&lt;/p&gt;
&lt;p&gt;obj.(&amp;quot;Meth&amp;quot; + &amp;quot;od&amp;quot;)();&lt;/p&gt;
&lt;p&gt;obj.&amp;quot;Property&amp;quot; = value;&lt;/p&gt;
&lt;p&gt;object value = obj.(&amp;quot;Prop&amp;quot; + &amp;quot;erty&amp;quot;);&lt;/p&gt;
&lt;p&gt;The return value from a dynamically invoked method or property getter would always be of type object.&lt;/p&gt;
&lt;p&gt;On the topic of class-level virtual methods, if you accept that in the C# world &amp;quot;static&amp;quot; no longer means &amp;quot;defined at compile time&amp;quot; but rather &amp;quot;associated with a class rather than an instance&amp;quot;, how about saying that a method can be defined &amp;quot;static virtual&amp;quot; to have the behavior people want?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3450190" width="1" height="1"&gt;</description></item><item><title>re: Calling static methods on type variables is illegal, part three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2007/06/21/calling-static-methods-on-type-parameters-is-illegal-part-three.aspx#3449170</link><pubDate>Fri, 22 Jun 2007 01:29:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3449170</guid><dc:creator>Barry.Kelly</dc:creator><description>&lt;p&gt;I work on the Delphi compiler at CodeGear.&lt;/p&gt;
&lt;p&gt;Delphi class methods are instance methods on a metaclass. All classes have one and only one metaclass instance, and a class type identifier is a literal for accessing that value in an expression. &lt;/p&gt;
&lt;p&gt;Constructors are effectively class methods, but with special magic to handle things like allocating an instance. This means that constructors can be virtual; this can be taken advantage of with variables of a metaclass type. Variables of a metaclass type are a lot like a runtime kind of type parameter, and can similarly be restricted to descend from a given ancestor. For example:&lt;/p&gt;
&lt;p&gt;---8&amp;lt;---&lt;/p&gt;
&lt;p&gt;type&lt;/p&gt;
&lt;p&gt; &amp;nbsp;T = class end;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;U = class(T) end;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Tc = class of T;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Uc = class of U;&lt;/p&gt;
&lt;p&gt;var&lt;/p&gt;
&lt;p&gt; &amp;nbsp;x: Tc;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;y: Uc;&lt;/p&gt;
&lt;p&gt;begin&lt;/p&gt;
&lt;p&gt; &amp;nbsp;x := T; // ok&lt;/p&gt;
&lt;p&gt; &amp;nbsp;x := U; // ok&lt;/p&gt;
&lt;p&gt; &amp;nbsp;y := T; // not ok&lt;/p&gt;
&lt;p&gt; &amp;nbsp;y := U; // ok&lt;/p&gt;
&lt;p&gt; &amp;nbsp;x := y; // ok&lt;/p&gt;
&lt;p&gt; &amp;nbsp;x.M; // if T has method M, it can be called like this&lt;/p&gt;
&lt;p&gt;end.&lt;/p&gt;
&lt;p&gt;---&amp;gt;8---&lt;/p&gt;
&lt;p&gt;Class methods are basically implemented on .NET as instance methods of a singleton static final field associated with the type, looking roughly like '@MetaFoo' for a class 'Foo'.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3449170" width="1" height="1"&gt;</description></item></channel></rss>