<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Jon Skeet: Coding Blog : Google</title><link>http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx</link><description>Tags: Google</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>MVP Again</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/10/30/mvp-again.aspx</link><pubDate>Fri, 30 Oct 2009 21:14:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1736410</guid><dc:creator>skeet</dc:creator><slash:comments>28</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1736410</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1736410</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/10/30/mvp-again.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m delighted to be able to announce that I&amp;#39;m now an MVP again.&lt;/p&gt;  &lt;p&gt;Google has reconsidered the situation and worked out a compromise: I now receive no significant gifts from Microsoft, and I&amp;#39;m not under NDA with them. While that precludes me from a lot of MVP activities, it removes any concerns to do with Google&amp;#39;s &lt;a href="http://investor.google.com/conduct.html#III"&gt;Code of Conduct&lt;/a&gt;. Basically my MVP status is truly just a token of Microsoft&amp;#39;s recognition of what I&amp;#39;ve done in the C# community - and that&amp;#39;s fine by me.&lt;/p&gt;  &lt;p&gt;When I &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/10/01/mvp-no-more.aspx"&gt;announced&lt;/a&gt; that I&amp;#39;d been advised not to seek renewal, I was amazed at the scale of the reaction in the comments, other blog posts, Twitter and personal email. I was touched by the response of the community. I really love working at Google, and the fact that we could figure out a solution to this situation is definitely one of the things that makes Google such an awesome place to work. Oh, and did I mention that &lt;a href="http://www.google.co.uk/intl/en/jobs/index.html"&gt;we&amp;#39;re hiring&lt;/a&gt;? :)&lt;/p&gt;  &lt;p&gt;Anyway, the basic message of this post is: thanks to the community for caring, thanks to Google for reconsidering, and thanks to Microsoft for renewing my award. And they all lived happily ever after...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1736410" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category></item><item><title>OS Jam at Google London: C# 4 and the DLR</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/06/19/os-jam-at-google-london-c-4-and-the-dlr.aspx</link><pubDate>Fri, 19 Jun 2009 16:50:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1695865</guid><dc:creator>skeet</dc:creator><slash:comments>19</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1695865</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1695865</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2009/06/19/os-jam-at-google-london-c-4-and-the-dlr.aspx#comments</comments><description>&lt;p&gt;Last night I presented for the first time at the &lt;a href="http://osjam.appspot.com/"&gt;Google Open Source Jam&lt;/a&gt; at our offices in London. The room was packed, but only a very few attendees were C# developers. I know that C# isn&amp;#39;t the most popular language on the Open Source scene, but I was still surprised there weren&amp;#39;t more people using C# for their jobs and hacking on Ruby/Python/etc at night.&lt;/p&gt;
&lt;p&gt;All the talks at OSJam are just 5 minutes long, with 2 minutes for questions. I&amp;#39;m really not used to this format, and felt extremely rushed... however, it was still a lot of fun. I used a somewhat different approach to my slides than the normal &amp;quot;bullet points in PowerPoint&amp;quot; - and as it was only short, I thought I might as well effectively repeat the presentation here in digital form. (Apologies if the images are an inconvenient size for you. I tried a few different ones, and this seemed about right. Comments welcome, as I may do a similar thing in the future.)&lt;/p&gt;
&lt;table border="0" cellspacing="0" cellpadding="2"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image001" border="0" alt="First slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image001.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Introductory slide. Colleagues forced me to include the &lt;a href="http://askjonskeet.com"&gt;askjonskeet.com&lt;/a&gt; link. &lt;/p&gt;
&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image002" border="0" alt="Second slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image002.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source. You can &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx"&gt;debug through a lot of the source code&lt;/a&gt; for the framework if you agree to a &amp;quot;reference licence&amp;quot;, but it&amp;#39;s not quite the same thing.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image003" border="0" alt="Third slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image003.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;.NET isn&amp;#39;t Open Source, but the DLR is. And IronRuby. And IronPython. Yay!&lt;/p&gt;
&lt;p&gt;And of course &lt;a href="http://mono-project.com"&gt;Mono&lt;/a&gt;&amp;nbsp;is Open Source: the DLR and Mono&amp;nbsp;play nicely together, and the Mono team is hoping to implement the new C# 4.0 features for the 2.8 release in roughly the same timeframe as Microsoft.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image004" border="0" alt="Fourth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image004.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;This is what .NET 4.0 will look like. The DLR will be included in it, despite being open source. IronRuby and IronPython aren&amp;#39;t included, but depend heavily on the DLR. (Currently available versions allow you to use a &amp;quot;standalone&amp;quot; DLR or the one in .NET 4.0b1.) &lt;/p&gt;
&lt;p&gt;C# doesn&amp;#39;t really depend on the DLR except for its handling of &lt;code&gt;dynamic&lt;/code&gt;. C# is a statically typed language, but C# 4.0 has a new static type called &lt;code&gt;dynamic&lt;/code&gt; which you can do just about anything with. (This got a laugh, despite being a simple and mostly accurate summary of the dynamic typing support in C# 4.0.)&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image005" border="0" alt="Fifth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image005.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;The fundamental point of the DLR is to handle &lt;em&gt;call sites&lt;/em&gt; - decide what to do dynamically with little bits of code. Oh, and do it quickly. That&amp;#39;s what the caches are for. They&amp;#39;re really clever - particularly the L0 cache which compiles rules (about the context in which a particular decision is valid) into IL via dynamic methods. Awesome stuff.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m sure the DLR does many other snazzy things, but this feels like it&amp;#39;s the core part of it. &lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image006" border="0" alt="Sixth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image006.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;At execution time, the relevant &lt;em&gt;binder&lt;/em&gt; is used to work out what a call site should actually do. Unless, that is, the call has a target which implements the shadowy &lt;code&gt;IDynamicMetaObjectProvider&lt;/code&gt; interface (winner of &amp;quot;biggest mouthful of a type name&amp;quot; prize, 2009) - in which case, the object is asked to handle the call. Who knows what it will do?&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image007" border="0" alt="Seventh slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image007.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;
&lt;p&gt;Beautifully syntax-highlighted C# 4.0 source code showing the &lt;code&gt;dynamic&lt;/code&gt; type in action. The method calls on lines 2 and 3 are both dynamic, even though in the latter case it&amp;#39;s just using a static method. Which overload will it pick? It all depends on the type of the actual value at execution time.&lt;/p&gt;
&lt;p&gt;If I&amp;#39;d had more time, I&amp;#39;d have demonstrated how the C# compiler preserves the static type information it knows at compile time for the execution time binder to use. This is very cool, but would take far too long to demonstrate in this talk - especially to a bunch of non-C# developers.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="center"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;margin-left:0px;border-left-width:0px;margin-right:0px;" title="Image008" border="0" alt="Eighth slide" src="http://pobox.com/~skeet/csharp/blogfiles/osjam_20090618/Image008.jpg" width="500" height="360" /&gt;&lt;/td&gt;
&lt;td valign="center"&gt;There were a couple of questions, but I can&amp;#39;t remember them offhand. Someone asked me afterwards about how all this worked on non-.NET implementations (i.e. Mono, basically). I gather the DLR itself works, but I don&amp;#39;t know whether C# code compiled in the MS compiler will work at the moment - it embeds references to binder types in Microsoft.CSharp.dll, and I don&amp;#39;t know what the story is about that being supported on Mono.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This is definitely the format I want to use for future presentations. It&amp;#39;s fun to write, fun to present, and I&amp;#39;m sure the &amp;quot;non-professionalism&amp;quot; of it makes it a lot more interesting to watch. Although it&amp;#39;s slower to create text-like slides (such as the first and the last one) this way, the fact that I don&amp;#39;t need to find clip-art or draw boxes with painful user interfaces is a definite win - especially as I&amp;#39;m going to try to be much more image-biased from now on. (I don&amp;#39;t want people reading slides while I&amp;#39;m talking - they should be listening, otherwise it&amp;#39;s just pointless.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1695865" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category></item><item><title>DotNetRocks interview</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/10/07/dotnetrocks-interview.aspx</link><pubDate>Tue, 07 Oct 2008 16:33:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1650011</guid><dc:creator>skeet</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1650011</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1650011</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/10/07/dotnetrocks-interview.aspx#comments</comments><description>&lt;p&gt;Last Monday evening I had a chat with the guys from &lt;a href="http://dotnetrocks.com"&gt;DotNetRocks&lt;/a&gt;, and today &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=383"&gt;the show has gone live&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;I wouldn&amp;#39;t claim to have said anything &lt;em&gt;particularly&lt;/em&gt; earth-shattering, and regular readers will probably be familiar with many of the themes anyway, but I thoroughly enjoyed it and hope you will too. Amongst other things, we talked about:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Protocol buffers&lt;/li&gt; &lt;li&gt;Implicit typing and anonymous types&lt;/li&gt; &lt;li&gt;Why it doesn&amp;#39;t bother me that Office hasn&amp;#39;t been ported to .NET&lt;/li&gt; &lt;li&gt;C# 4&lt;/li&gt; &lt;li&gt;My wishlist for C#&lt;/li&gt; &lt;li&gt;Threading and Parallel Extensions&lt;/li&gt; &lt;li&gt;Working for Google&lt;/li&gt; &lt;li&gt;How to learn LINQ&lt;/li&gt; &lt;li&gt;C# in Depth&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Feedback welcome. And yes, I know I sound somewhat like a stereotypical upper-class idiot at times. Unfortunately there&amp;#39;s not a lot I can do about that. Only the &amp;quot;idiot&amp;quot; part is accurate :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1650011" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+4/default.aspx">C# 4</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Parallelisation/default.aspx">Parallelisation</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 4: static interfaces</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx</link><pubDate>Fri, 29 Aug 2008 18:50:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646206</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646206</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646206</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;Warning:&lt;/b&gt; During this entire post, I will use the word &lt;i&gt;static&lt;/i&gt; to mean &amp;quot;relating to a type instead of an instance&amp;quot;. This isn&amp;#39;t a &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Static+Methods/default.aspx"&gt;strictly accurate use&lt;/a&gt; but I believe it&amp;#39;s what most developers actually think of when they hear the word.&lt;/p&gt; &lt;p&gt;A few members of the interfaces in Protocol Buffers have no logical reason to act on instances of their types. The message interface has members to return the message&amp;#39;s type descriptor (the PB equivalent of &lt;code&gt;System.Type&lt;/code&gt;), the default instance for the message type, and a builder for the message type. The builder interface copies the first two of these, and also has a method to create a builder for a particular field. None of these touch any instance data.&lt;/p&gt; &lt;p&gt;In most cases this doesn&amp;#39;t actually cause any difficulties - we usually have an instance available when we&amp;#39;re in the PB library code, and the generated types have static properties for the default instance and the type descriptor anyway. Even so, it feels messy to have interface members which rely only on the &lt;i&gt;type&lt;/i&gt; of the implementation and not on any of the actual &lt;i&gt;data&lt;/i&gt; of the instance.&lt;/p&gt; &lt;p&gt;I&amp;#39;ve wondered before now about the possibility of having static members in interfaces - usually when thinking about plug-in architectures - but there&amp;#39;s always been the problem of working out how to specify the type on which to call the members. Variables and other expressions usually refer to values rather than types, and &lt;code&gt;System.Type&lt;/code&gt; doesn&amp;#39;t help as it provides no compile-time knowledge of the type being referred to.&lt;/p&gt; &lt;p&gt;There&amp;#39;s one big exception to this, however: generic type parameters. I don&amp;#39;t know why it had never occurred to me before, but this is a great fit for the ability to safely call static methods on types which are unknown at compile-time. Furthermore, it could provide a great way of enforcing the presence of constructors with appropriate signatures, and even operators. Before I get too far ahead of myself, let&amp;#39;s tie the simple case to a concrete example.&lt;/p&gt; &lt;h3&gt;Creating builders from nothing&lt;/h3&gt; &lt;p&gt;In my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx"&gt;previous post&lt;/a&gt; I gave an example of a method which ideally wanted to return a new message given a &lt;code&gt;CodedInputStream&lt;/code&gt; and an &lt;code&gt;ExtensionRegistry&lt;/code&gt; (both types within Protocol Buffers, the details of which are unimportant to this example). The current code looks like this:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;For the purposes of this discussion I&amp;#39;ll simplify it a little, making it a generic method in a non-generic type.&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;The first parameter is a function which will return us a builder. We can&amp;#39;t simply add a &lt;code&gt;new()&lt;/code&gt; constraint to &lt;code&gt;TBuilder&lt;/code&gt; as not all geenrated builders will have a public constructor. However, we &lt;i&gt;do&lt;/i&gt; know that the &lt;code&gt;TMessage&lt;/code&gt; type has a &lt;code&gt;CreateBuilder()&lt;/code&gt; method because it implements &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt;. Unfortunately we don&amp;#39;t have an instance of &lt;code&gt;TMessage&lt;/code&gt; to call &lt;code&gt;CreateBuilder()&lt;/code&gt; on! Really, we&amp;#39;d like to be able to change the code to this: &lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage, TBuilder&amp;gt; (CodedInputStream input, ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = &lt;b&gt;TMessage.CreateBuilder();&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;That&amp;#39;s currently impossible, but only because we can&amp;#39;t specify static methods in interfaces. Suppose we could write:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; TBuilder CreateBuilder();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Other methods as before&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Wouldn&amp;#39;t that be useful? The value would almost entirely be for generic types or methods where the type parameter is constrained to specify the relevant interface, but that could arguably still be very handy.  &lt;h3&gt;Operators and constructors&lt;/h3&gt; &lt;p&gt;At this point hopefully the idea I mentioned earlier of being able to specify operators and constructors is quite obvious. For instance, we could make all the existing numeric types implement &lt;code&gt;IArithmetic&amp;lt;T&amp;gt;&lt;/code&gt; (where &lt;code&gt;T&lt;/code&gt; was the same type, e.g. &lt;code&gt;int : IArithmetic&amp;lt;int&amp;gt;&lt;/code&gt;):&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; +(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; -(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; /(T left, T right);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt; T &lt;span class="Keyword"&gt;operator&lt;/span&gt; *(T left, T right);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used in LINQ to Objects, for example:&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; T Sum&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source) &lt;span class="Linq"&gt;where&lt;/span&gt; T : IArithmetic&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T total = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T element &lt;span class="Statement"&gt;in&lt;/span&gt; source)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total += element; &lt;span class="InlineComment"&gt;// Interface says we can do total + element&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; total;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;Plug-ins for a particular program could implement &lt;code&gt;IPlugin&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt; (PluginHost host);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal plug-in members here&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; Title { get; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// Used within a PluginHost like this...&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; T CreatePlugin&amp;lt;T&amp;gt;() &lt;span class="Linq"&gt;where&lt;/span&gt; T : IPlugin&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T plugin = &lt;span class="Keyword"&gt;new&lt;/span&gt; T(&lt;span class="Keyword"&gt;this&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; log.Info(&lt;span class="String"&gt;&amp;quot;Loaded plugin {0}&amp;quot;&lt;/span&gt;, plugin.Title);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; plugin;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;In fact, I&amp;#39;d imagine it would make sense to define a whole family of &lt;code&gt;IConstructable&lt;/code&gt; interfaces, along the same lines as the &lt;code&gt;Func&lt;/code&gt; and &lt;code&gt;Action&lt;/code&gt; delegate families:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T arg)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IConstructable&amp;lt;T1, T2&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;nbsp;&lt;span class="Keyword"&gt;new&lt;/span&gt;(T1 arg1, T2 arg2);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="InlineComment"&gt;// etc&lt;/span&gt; &lt;/div&gt; &lt;h3&gt;Inheritance raises its ugly head&lt;/h3&gt; &lt;p&gt;There&amp;#39;s a fly in the ointment here. Normally, if a base type implements an interface, that means a type derived from it will effectively implement the interface too. That ceases to hold in all cases. You can get away with it for straightforward static methods/properties, in the same way that people often write code such as &lt;code&gt;UTF8Encoding.UTF8&lt;/code&gt; when they really just mean &lt;code&gt;Encoding.UTF8&lt;/code&gt;. However, it doesn&amp;#39;t work for constructors - they aren&amp;#39;t inherited, so you can&amp;#39;t guarantee that &lt;code&gt;Banana&lt;/code&gt; has a parameterless constructor just because &lt;code&gt;Fruit&lt;/code&gt; does. &lt;/p&gt; &lt;p&gt;This is not only a problem for one concrete class deriving from another, but also for abstract implementations of interfaces. This happens a lot in Protocol Buffers; often the interface is partially implemented by the abstract class, with the final &amp;quot;leaf&amp;quot; classes in the inheritance tree implementing outstanding members and occasionally overriding earlier implementations for the sake of efficiency. Should we be able to specify an abstract static method in the abstract class, making sure that there&amp;#39;s an appropriate implementation by the time we hit a concrete class? As it happens that would be useful elsewhere in the Protocol Buffer library, but I&amp;#39;ll admit it&amp;#39;s slightly messy. I suspect there are ways round all of these issues, even if they might sometimes involve restricting the feature to particular, common use cases. However, every niggle would add its own piece of complexity. &lt;/p&gt; &lt;p&gt;There may well be other issues which would prove challenging - and other interesting aspects such as what explicit interface implementation would mean, if anything, in the context of static members. Language experts may well be able to reel off great lists of problems - I&amp;#39;d be very interested to here the ensuing discussions. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I believe that static interface members could prove very useful in generic algorithms, particularly if operators and constructors were allowed as well as the existing member types available in interfaces. There are significant sticking points to be carefully considered, and I wouldn&amp;#39;t like to prejudge the outcome of such deliberations in terms of whether the feature would be useful enough to merit the additional language complexity involved. It feels odd to implement an interface member which is effectively only of use when the implementing type is being used as the type argument for a generic type or method, but as developers learn to think more generically that may be less of a restriction than it currently seems. &lt;/p&gt; &lt;p&gt;This post is the last in my current batch talking about Protocol Buffers. There may well be more, but it&amp;#39;s unlikely now that the Protocol Buffer port is almost entirely complete. Most of these posts have involved generics, and the current limitations of what C# allows us to express in them. I do not intend to give the impression that I&amp;#39;m dissatisfied with C# - I&amp;#39;ve just found it interesting to take a look at what lies beyond the current boundaries of the language. The one aspect of these posts which I &lt;i&gt;would&lt;/i&gt; definitely like to see addresses is that of covariant return types - the Java implementation of Protocol Buffers is significantly simpler in many ways &lt;i&gt;purely&lt;/i&gt; due to this one point.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646206" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Lessons learned from Protocol Buffers, part 3: generic type relationships</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx</link><pubDate>Fri, 29 Aug 2008 18:48:48 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646204</guid><dc:creator>skeet</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1646204</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1646204</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-3-generic-type-relationships.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-2-self-referential-generic-types.aspx"&gt;part 2&lt;/a&gt; of this series we saw how the message and builder interfaces were self-referential in order to allow the implementation types to be part of the API. That&amp;#39;s one sort of relationship, but in this post we&amp;#39;ll see how the two interfaces relate to each other. If you remember from &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/20/lessons-learned-from-protocol-buffers-part-1-messages-builders-and-immutability.aspx"&gt;part 1&lt;/a&gt; every generated message type has a corresponding builder type. As it happens, this is implemented with a nested type, so if you had a &lt;code&gt;Person&lt;/code&gt; message, the generated types would be &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; (in a specified namespace, of course).&lt;/p&gt; &lt;p&gt;Without any interfaces involved, this would be very simple. The types would just look like this (with more members, of course):&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;You may well be wondering why there are two methods for creating a builder. The static method is convenient for code which knows it&amp;#39;s dealing with the &lt;code&gt;Person&lt;/code&gt; message. The instance method ends up being part of the message interface, which makes it useful for code which can work with any message. In addition, the constructor for &lt;code&gt;Person.Builder&lt;/code&gt; is accessible in the C# version. In the original Java code the only way of creating a builder is via the methods in the message class; I decided to remove this restriction for the sake of making the oh-so-readable object initializer syntax available in C# 3.&lt;/p&gt; &lt;h3&gt;Redesigning the interfaces to refer to each other&lt;/h3&gt; &lt;p&gt;In part 2 we created self-referential interfaces for the message and builder interfaces which looked like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The constraints on the type parameters allow us to make the API very specific, and we can use the same trick again when we relate the builder and message types together. The step where we introduce a new type parameter to each of them is straightforward:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Unfortunately without any restrictions on the &amp;quot;foreign&amp;quot; type parameter in each interface, we don&amp;#39;t get enough information to make everything work. We need to tie the two types together more tightly, like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;To make this concrete for &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Person.Builder&lt;/code&gt; we end up with implementations like this:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Person : IMessage&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; Builder CreateBuilder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder CreateBuilderForType() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Builder : IBuilder&amp;lt;Person, Builder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Builder() { ... }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Modifier"&gt;public&lt;/span&gt; Person Build() { ... }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This works, but it&amp;#39;s really ugly. Any generic methods wanting to take a &lt;code&gt;TMessage&lt;/code&gt; type parameter implementing &lt;code&gt;IMessage&amp;lt;TMessage, TBuilder&amp;gt;&lt;/code&gt; have to also have a &lt;code&gt;TBuilder&lt;/code&gt; type parameter, and the two constraints need to be expressed each time. It&amp;#39;s a real pain. In fact, I&amp;#39;ve got an &lt;code&gt;IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt; interface which contains almost nothing in it (and which the more generic interface extends). This allows me to get hold of the message type (and use it in the API), inferring the builder type by reflection. That&amp;#39;s a pain too, frankly. It&amp;#39;s a particular nuisance because when I &lt;i&gt;do&lt;/i&gt; infer the builder type, I haven&amp;#39;t actually got any compile-time constraint the lets any other code know that it&amp;#39;s the right builder type for the message type. In one specific case it&amp;#39;s led to this horrific method (in a type generic in &lt;code&gt;TMessage&lt;/code&gt;:&lt;/p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl&amp;lt;TMessage2, TBuilder&amp;gt; (Func&amp;lt;TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage2 : TMessage, IMessage&amp;lt;TMessage2, TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&lt;/div&gt; &lt;p&gt;Fortunately this is hidden from public view - and the only reason to do it at all is to enable a pleasant API of &lt;code&gt;MessageStreamIterator&amp;lt;TMessage&amp;gt; : IEnumerable&amp;lt;TMessage&amp;gt; where TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;/code&gt;. The result of the evil method above is exactly what the caller is likely to want, otherwise I wouldn&amp;#39;t put up with it. However, that sort of excuse has been coming up far too much in the PB implementation, so I&amp;#39;ve had a quick think about what could be done about it.&lt;/p&gt; &lt;h3&gt;Contemplating a more expressive language&lt;/h3&gt; &lt;p&gt;I should really prefix this section by saying that I&amp;#39;m not actually &lt;em&gt;suggesting&lt;/em&gt; this as a way forward for C# or .NET. (I suspect it would take more work in the CLR as well as just in the language; I don&amp;#39;t know enough about CLR generics to say for sure, but I&amp;#39;d be surprised if this were feasible.) I haven&amp;#39;t encountered many situations where I&amp;#39;ve wanted anything like this, and the extra complexity in the language would be quite high, I suspect. Suppose an interface could contain extra type parameters, including constraints, in the body of the interface:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Attention"&gt;// Purely imaginary syntax!&lt;/span&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IMessage&amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;, TBuilder.TMessage : TMessage&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TBuilder&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IBuilder&amp;lt;TBuilder&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TBuilder : IBuilder&amp;lt;TBuilder&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TMessage&amp;gt; &lt;span class="Linq"&gt;where&lt;/span&gt; TMessage : IMessage&amp;lt;TMessage&amp;gt;, TMessage.TBuilder : TBuilder&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="InlineComment"&gt;// Normal methods, which could use TMessage&lt;/span&gt;&lt;br /&gt;} &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;There are various ways in which the interface implementation could indicate the type of TBuilder. The syntax itself isn&amp;#39;t particularly interesting - it&amp;#39;s the extra information which is conveyed which is the important bit. I&amp;#39;ve dithered between this being a step forward and it not. At first glance it looks no better than having both type parameters in the interface declaration, but I believe it would genuinely make a difference. For instance, the above evil method could be written as:&lt;/p&gt; &lt;p&gt; &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span class="Modifier"&gt;static&lt;/span&gt; TMessage BuildImpl(Func&amp;lt;TMessage.TBuilder&amp;gt; builderBuilder,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CodedInputStream input,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtensionRegistry registry) &lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TMessage.TBuilder builder = builderBuilder();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input.ReadMessage(builder, registry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="Statement"&gt;return&lt;/span&gt; builder.Build();&lt;br /&gt;}&amp;nbsp; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;This time there&amp;#39;s no need for the method to be generic, because the type is already generic in the message type. Furthermore, we can &lt;i&gt;call&lt;/i&gt; this method with no reflection. All other APIs which have previously had to be specify two type parameters can now just specify the one. Apart from anything else, this leaves more scope for type inference in generic methods - passing &lt;i&gt;either&lt;/i&gt; a message &lt;i&gt;or&lt;/i&gt; a builder to a generic method happens occasionally, but it&amp;#39;s very rare to pass in both.&lt;/p&gt; &lt;p&gt;We&amp;#39;ve essentially expressed the relationship between the message type and the builder type a little more explicitly, so that we can guarantee it exists (and use it) at compile time. That&amp;#39;s at the heart of the problem to start with - without a second type parameter in the initial interface declaration, in the current language there&amp;#39;s no way of expressing a close relationship with another type. &lt;/p&gt; &lt;h3&gt;Conclusion&lt;/h3&gt; &lt;p&gt;I don&amp;#39;t think it would be fair to say that C# really lets us down here - it happens not to support a pretty rare scenario, and that&amp;#39;s fair enough. I&amp;#39;d be interested to know whether any other languages allow the same sort of concepts to be expressed more pleasantly. The ugly solution I&amp;#39;ve presented here does at least work, and it&amp;#39;s nearly invisible to most users, who are likely to just reference the concrete generated types. I&amp;#39;m not happy with the verbosity which has become necessary in many places, but it&amp;#39;s in a good cause. It&amp;#39;s interesting to note that the Java API doesn&amp;#39;t use this sort of doubly-generic relationship: again, covariant return types allow the concrete message and builder types to express their APIs directly and still implement a more general interface at the same time.&lt;/p&gt; &lt;p&gt;In the next part I&amp;#39;ll look at another possibility which would make interfaces and generics a more powerful combination: static interface methods.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646204" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDevCenter/default.aspx">CSharpDevCenter</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/CSharpDev/default.aspx">CSharpDev</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Protocol+Buffers/default.aspx">Protocol Buffers</category></item><item><title>Google release protocol buffers as an open source project</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/07/08/google-release-protocol-buffers-as-an-open-source-project.aspx</link><pubDate>Tue, 08 Jul 2008 18:30:13 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1639821</guid><dc:creator>skeet</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1639821</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1639821</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2008/07/08/google-release-protocol-buffers-as-an-open-source-project.aspx#comments</comments><description>&lt;p&gt;Yesterday the &lt;a href="http://google-opensource.blogspot.com/2008/07/protocol-buffers-googles-data.html"&gt;Google open source blog announced&lt;/a&gt; a new open source project, to release one of the core pieces of Google infrastructure: &lt;a href="http://code.google.com/p/protobuf/"&gt;protocol buffers&lt;/a&gt;. Basically protocol buffers are a way of encoding structured data in a language-neutral and versioning-friendly fashion. Yes, there are a lot of similar ways of doing similar things, but this happens to be the one we use.&lt;/p&gt; &lt;p&gt;Currently the protocol buffer compiler (which takes .proto files and converts them into source for various languages) supports C++, Java and Python. I&amp;#39;m hoping to add C# to that list reasonably quickly. To start with this will just be in my spare time rather than as a 20% project, but who knows...&lt;/p&gt; &lt;p&gt;It&amp;#39;s really nice to work in an environment where the question of &amp;quot;is there any reason we can&amp;#39;t open source this?&amp;quot; crops up quite frequently. I&amp;#39;m looking forward to the day when a piece of code I&amp;#39;ve developed at Google ends up as part of an open source project.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1639821" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Google/default.aspx">Google</category></item></channel></rss>