<?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>Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx</link><description>As I mentioned a while back, there are some bugs in the compiler code which analyzes whether a set of classes violates the &amp;ldquo;no cycles&amp;rdquo; rules for base classes. (That is, a class is not allowed to inherit from itself, directly or indirectly</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9976120</link><pubDate>Wed, 10 Mar 2010 09:51:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9976120</guid><dc:creator>runefs</dc:creator><description>&lt;P&gt;I'm wondering why you're not simply applying a graph library where every node in a directed graph would be a type and every edge from that node would represent a direct dependency to the connected node. Then search for cycles in the graph. I'm sure there are reasons I would just find them interesting to read about&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;That would answer the question "are there any types with cycles in their base classes?" The question I actually needed to answer was "does this particular type declaration have cycles in its base class?" I can answer that question by simply constructing the set of dependencies; no need to build a graph of all types.&lt;/P&gt;
&lt;P&gt;I recently implemented &lt;A href="http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx"&gt;Andrew Kennedy's algorithm for identifying infinitary types given a set of nominal base type relations&lt;/A&gt;; in that algorithm I use something like the approach you suggest. -- Eric &lt;/P&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9976120" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9965325</link><pubDate>Wed, 17 Feb 2010 20:43:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9965325</guid><dc:creator>Alois Kraus</dc:creator><description>&lt;p&gt;I would like the ~&amp;gt; operator for null checks. What syntax it has ~&amp;gt; or .? is a non issue. Is this &amp;quot;automatic&amp;quot; null check seriously considered for version next of C#? I do not know who did initially have the idea &amp;nbsp;of the .? operator. But I have written about this quite some time ago at Connect and at my blog: &lt;a rel="nofollow" target="_new" href="http://geekswithblogs.net/akraus1/archive/2009/11/22/136473.aspx"&gt;http://geekswithblogs.net/akraus1/archive/2009/11/22/136473.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Yours,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Alois Kraus&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9965325" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9963400</link><pubDate>Mon, 15 Feb 2010 02:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963400</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; Bit Flag Check (yea, only saves a few keystrokes)&lt;/p&gt;
&lt;p&gt;&amp;gt; bool isWuzzleAndWaddle = myEnumVal ~&amp;gt; MyEnum.Wuzzle | MyEnum.Waddle;&lt;/p&gt;
&lt;p&gt;In .NET 4, there's a library-provided shorthand:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool isWuzzleAndWaddle = Enum.HasFlag(myEnumVal, MyEnum.Wuzzle | MyEnum.Waddle)&lt;/p&gt;
&lt;p&gt;see &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag"&gt;http://msdn.microsoft.com/en-us/library/system.enum.hasflag&lt;/a&gt;(VS.100).aspx&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963400" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9963193</link><pubDate>Sun, 14 Feb 2010 02:48:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963193</guid><dc:creator>Dave Sexton</dc:creator><description>&lt;p&gt;Recursive Lambda (compiler automatically creates IL as a non-overflowing Stack&amp;lt;T&amp;gt;, optimized with tail-recursion, etc.)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var factorial = a ~&amp;gt; (a &amp;gt; 1) ? a * self(a - 1) : a;&lt;/p&gt;
&lt;p&gt;(Yea, I just assumed a &amp;quot;self&amp;quot; keyword as well :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963193" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9963144</link><pubDate>Sat, 13 Feb 2010 21:30:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963144</guid><dc:creator>Dave Sexton</dc:creator><description>&lt;p&gt;Reflection (unlike dynamic, has static checking; enables IntelliSense in VS): &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var x = foo~&amp;gt;privateX;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var result = foo~&amp;gt;PrivateBar(x, y, z);&lt;/p&gt;
&lt;p&gt;Code as Data (ExpressionTree)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ExpressionTree parse ~&amp;gt; System.Int32.TryParse;&lt;/p&gt;
&lt;p&gt;Bit Flag Check (yea, only saves a few keystrokes):&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;bool isWuzzleAndWaddle = myEnumVal ~&amp;gt; MyEnum.Wuzzle | MyEnum.Waddle;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963144" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9963072</link><pubDate>Sat, 13 Feb 2010 12:30:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963072</guid><dc:creator>Erik Källén</dc:creator><description>&lt;p&gt;@Pavel: I don't think the change is trivial. However, C# 4 focused on those people that want dynamic abilites, now I think it is us &amp;quot;staticist&amp;quot;s turn. Regarding [NotNull], not good enough since I want the compiler to complain. Throwing a NullReferenceException would only be a fallback mechanism where another solution is not practical/possible.&lt;/p&gt;
&lt;p&gt;In 99.9 percent of cases, it would be used in the style of&lt;/p&gt;
&lt;p&gt;var arg = GetSomeArg();&lt;/p&gt;
&lt;p&gt;MyClass! object = GetMyObject(arg);&lt;/p&gt;
&lt;p&gt;in which case I want the compiler to say:&lt;/p&gt;
&lt;p&gt;argument 1: Cannot convert from SomeType to SomeType! an explicit cast exists.&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;return value: Cannot convert MyClass to MyClass!, an explicit cast exists.&lt;/p&gt;
&lt;p&gt;All other uses are just things that need to be worked around by the compiler writer, with a NullPointerException as a last resort. I'm certain that if they spend their time on doing this, they'll find a good way.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963072" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9963054</link><pubDate>Sat, 13 Feb 2010 11:00:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9963054</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;@Erik: but isn't the whole point of a non-null reference type to not have null as a possible value in the first place (thus eliminating NullReferenceException, and all other associated runtime exceptions)? I dare say that a type that is kind-of not null but not really - when you can trivially work around it, and get the old behavior - isn't worth it.&lt;/p&gt;
&lt;p&gt;If you just want to throw ArgumentException for null function arguments, then something like [NotNull] attribute specifically for them (and not a whole new type) is probably a better idea. And you can have that alread with PostSharp. Or, in 4.0, use DbC / System.Design.Contracts to validate arguments, including null checks (or,&lt;/p&gt;
&lt;p&gt;again, use PostSharp to auto-generate contracts from [NotNull]).&lt;/p&gt;
&lt;p&gt;As for the tricky cases above - they can all be handled in one way or another; it just shows that the change isn't as trivial as it seems. For example, in the absence of a well-defined default value for reference types, you'd somehow need to make it so that static fields are not accessible before their initializers run - which would require changing the existing order-of-declaration rule, and introduce recursive dependency checks. For structs, you'd need a default constructor. For arrays, a way to specify the new value for all created elements - either just one value, or a lambda &amp;nbsp;that generates a value given an index. Etc.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9963054" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9962609</link><pubDate>Fri, 12 Feb 2010 14:46:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962609</guid><dc:creator>Erik Källén</dc:creator><description>&lt;p&gt;@Pavel: For all that I care, each of those examples could throw some exception. It could even be a NullReferenceException, in which case the codegen wouldn't even have to change, I guess.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962609" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9962448</link><pubDate>Fri, 12 Feb 2010 06:16:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962448</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;@Erik:&lt;/p&gt;
&lt;p&gt;It's kind of a trick question, but if you had non-nullable reference types (say, T!), what values would you expect this code to produce?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; object![] a = new object![1];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; a[0]; // ?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; struct Foo { object! x; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Foo f = new Foo();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; f.x; // ?&lt;/p&gt;
&lt;p&gt;and a few more tricky cases:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; class Base {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;abstract void Foo();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;Base() { Foo(); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; class Derived : Base {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;object! o = new object();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;override void Foo() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; o; // ? (assume call during instantiation)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; class Static {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; static object! x = y; // ?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; static object! y = new object();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962448" width="1" height="1"&gt;</description></item><item><title>re: Making the code read like the spec</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/02/08/making-the-code-read-like-the-spec.aspx#9962115</link><pubDate>Thu, 11 Feb 2010 18:33:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9962115</guid><dc:creator>  Erik Källén </dc:creator><description>&lt;p&gt;What was I thinking? Of course I mean &amp;quot;non-nullable reference types&amp;quot;. DOH.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9962115" width="1" height="1"&gt;</description></item></channel></rss>