<?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>Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx</link><description>In the last post on this topic i talked about how it was critical that by the time a non-null type was used it was provably assigned some non-null value. The cases that came up were specifically instance fields in classes. In that post i showed how would</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412043</link><pubDate>Tue, 26 Apr 2005 09:40:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412043</guid><dc:creator>Gatsby</dc:creator><description>You could make it illegal to reference a non-nullable static within a static constructor, thus breaking the dependency cycle.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412047</link><pubDate>Tue, 26 Apr 2005 10:09:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412047</guid><dc:creator>CyrusN</dc:creator><description>Gatsby: If we didn't allow access to the static fields in the static constructor, then how would we initialize the fields?&lt;br&gt;&lt;br&gt;Note, if you have:&lt;br&gt;&lt;br&gt;public static string! name = new Cyrus().name;&lt;br&gt;&lt;br&gt;then that code in the initializer just goes into the static constructor.&lt;br&gt;&lt;br&gt;So, without the static constructor, there is no way to initialize the static non-nullable fields.  And if you don't initialize them, then they certainly won't be &amp;quot;not&amp;quot; null :)</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412048</link><pubDate>Tue, 26 Apr 2005 10:09:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412048</guid><dc:creator>CyrusN</dc:creator><description>Gatsby: If we go down that path, that seems like my first option which is to just not allow non-nullable statics.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412053</link><pubDate>Tue, 26 Apr 2005 10:31:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412053</guid><dc:creator>Gatsby</dc:creator><description>Sorry, let me make that more precise: &amp;quot;In a static constructor, you cannot have anything non-nullable in the RHS of an expression, which cannot be proved to be non-null.&amp;quot; This doesn't stop you from assigning to A.staticNonNullableVar in the static constructor of A.&lt;br&gt;&lt;br&gt;However one variable that can't be proved to be non-null in your example is Person.name, namely because we can't know at compile time that Person hasn't been initialized yet. So in general, referencing non-nullable statics of other classes would not fly.&lt;br&gt;&lt;br&gt;Make sense?</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412055</link><pubDate>Tue, 26 Apr 2005 10:32:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412055</guid><dc:creator>Gatsby</dc:creator><description>And by 'RHS of an expression' I meant right hand side of an assignment operation. :)</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412063</link><pubDate>Tue, 26 Apr 2005 11:27:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412063</guid><dc:creator>CyrusN</dc:creator><description>Gatsby: So how do you initialize the value then? :)&lt;br&gt;&lt;br&gt;If you can't have a non-null value on the rhs, and that's *exactly* what you need to assign into the static field... then it seems like you're S.O.L. :)</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412097</link><pubDate>Tue, 26 Apr 2005 14:32:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412097</guid><dc:creator>Kamil Skalski</dc:creator><description>Well, I suppose that assigning an external non-nullable or literal isn't the only way to initialize the field. What about&lt;br&gt;&lt;br&gt;string! a;&lt;br&gt;&lt;br&gt;static Class () {&lt;br&gt;  string x = Foo ();&lt;br&gt;  if (x != null)&lt;br&gt;    a = x;&lt;br&gt;  else&lt;br&gt;    a = &amp;quot;error?&amp;quot;;&lt;br&gt;}&lt;br&gt;&lt;br&gt;I guess this kind of assigning to non-nullable types is a *must*, otherwise the only values inside the non-nullable types would have to be literal... or values obtained from external function, which would break static verifyability.&lt;br&gt;&lt;br&gt;I'm a *great* fan of non-nullable types, because they allow to natually use the framework to implement ML-like option['a] type. Simply None() becomes null and Some(x) becomes x - no need to create a wrapper. Of course we can do it now, but obviously we cannot expect compiler to forbid/warn about usage of A as A! (that is fetching element from option value). &lt;br&gt;&lt;br&gt;If entire BCL were decorated with non-nullable types, then we could utilize nullable types as 'unsure' values, which compiler would force/suggest to check before usage.&lt;br&gt;&lt;br&gt;StringBuilder! x = StringBuilder (&amp;quot;&amp;quot;);&lt;br&gt;string! s = x.ToString ();&lt;br&gt;...&lt;br&gt;string line = buffer.ReadLine ();&lt;br&gt;// warning, line might be null&lt;br&gt;Console.WriteLine (line.Substring (1));&lt;br&gt;&lt;br&gt;This is one of the those changes, which must be made *globally* in order to be usable, but then they will rule :-) (just like generics)</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412145</link><pubDate>Tue, 26 Apr 2005 17:32:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412145</guid><dc:creator>Ron Buckton</dc:creator><description>You could require that all non-null reference types are assigned a default value at declaration.  It would incur a penalty of the overhead of instantiating a possibly unneeded reference type but all of these other issues would be moot.&lt;br&gt;&lt;br&gt;Possibly not the best suggestion but its worth considering.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412231</link><pubDate>Tue, 26 Apr 2005 20:46:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412231</guid><dc:creator>CyrusN</dc:creator><description>Kamil: &amp;quot;Well, I suppose that assigning an external non-nullable or literal isn't the only way to initialize the field. What about &lt;br&gt;&lt;br&gt;static string! a; &lt;br&gt;&lt;br&gt;static Class () { &lt;br&gt;...string x = Foo (); &lt;br&gt;...if (x != null) &lt;br&gt;......a = x; &lt;br&gt;...else &lt;br&gt;......a = &amp;quot;error?&amp;quot;; &lt;br&gt;} &lt;br&gt;&amp;quot;&lt;br&gt;&lt;br&gt;The problem with this is that Foo() could execute arbitrary code.  That code could then end up trying to use &amp;quot;a&amp;quot; before it had been initialized.  Not good!&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;quot;I guess this kind of assigning to non-nullable types is a *must*, otherwise the only values inside the non-nullable types would have to be literal... or values obtained from external function, which would break static verifyability. &amp;quot;&lt;br&gt;&lt;br&gt;Yup.  And that limits the usefulness if we only allow non-null literals.  That means that all you can pretty much have are non-null strings :(</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412396</link><pubDate>Wed, 27 Apr 2005 06:25:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412396</guid><dc:creator>Joe Duffy</dc:creator><description>This is something Haskell got right and C[++|#|blah] got wrong. Nullability should be completely opt-in. Maybe&amp;lt;T&amp;gt; = Just T | nil works for me.&lt;br&gt;&lt;br&gt;Nullable&amp;lt;T&amp;gt; is close. But not quite.&lt;br&gt;&lt;br&gt;With that said, I love the idea of pre-/post-/invariant-condition annotations to the C# syntax. This is a great use of that style of programming.&lt;br&gt;&lt;br&gt;Of course, flippant, off topic crap like this probably isn't helpful. Nonetheless, great set of posts. Keep it coming.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412397</link><pubDate>Wed, 27 Apr 2005 06:26:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412397</guid><dc:creator>Joe Duffy</dc:creator><description>By the way, if it wasn't obvious: the &amp;quot;flippant, off-topic crap&amp;quot; comment above was in reference to my comment--not your post. :)</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412433</link><pubDate>Wed, 27 Apr 2005 08:32:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412433</guid><dc:creator>CyrusN</dc:creator><description>Joe: &amp;quot;This is something Haskell got right and C[++|#|blah] got wrong. Nullability should be completely opt-in. Maybe&amp;lt;T&amp;gt; = Just T | nil works for me.&amp;quot;&lt;br&gt;&lt;br&gt;I have to disagree with taht Joe.  The Haskell style of Maybe (also known as &amp;quot;Optional&amp;quot;, or in &lt;br&gt;other funtional languages) is a distinctly differnt concept than Nullable vs. Nonnullable in OO languages.  In OO Nullable follows the &amp;quot;is a&amp;quot; relationship.  So Nullable&amp;lt;T&amp;gt; is-a T.  In Haskell/Scheme/Ocaml, this is not the case with Maybe.  A Maybe&amp;lt;int&amp;gt; is not an int.&lt;br&gt;&lt;br&gt;Note: in C# i have my own &amp;quot;Maybe&amp;quot; class (which i call &amp;quot;Optional&amp;quot;).  I blogged about this before on this blog.   However, it serves a very different purpse than nullable.&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;quot;With that said, I love the idea of pre-/post-/invariant-condition annotations to the C# syntax. This is a great use of that style of programming. &amp;quot;&lt;br&gt;&lt;br&gt;Cool :)</description></item><item><title>Difficulties with non-nullable types (part 4)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412446</link><pubDate>Wed, 27 Apr 2005 09:36:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412446</guid><dc:creator>Cyrus' Blather</dc:creator><description>So far i've discussed two interesting problems that arise when you try to add non-nullable types to C#...</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412561</link><pubDate>Wed, 27 Apr 2005 15:39:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412561</guid><dc:creator>joc</dc:creator><description>In static constructors you could disallow access to static non-nullable fields (as well as static non-nullable properties, functions with static non-nullable return types or non-nullable out and ref parameters) of other types: e.g. inside the static constructor of Cyrus, the static fields of Person would not be accessible.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412672</link><pubDate>Wed, 27 Apr 2005 20:41:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412672</guid><dc:creator>Joe Duffy</dc:creator><description>Cyrus, I know nullability in OO differs from optionality in most functional languages. My point is that optional makes a helluva lot more sense than nullable baked into the type system.&lt;br&gt;&lt;br&gt;Null of T should never be considered to be a real T. That's ludicrous--it isn't true, first of all... I think type systems should not be in the business of lying--and gives way to crap like NullReferenceExceptions.&lt;br&gt;&lt;br&gt;I prefer to fix these styles of errors in the static type system than let dynamism run a good program at runtime. But nowadays, that seems to be an old skool point of view.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412704</link><pubDate>Wed, 27 Apr 2005 22:33:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412704</guid><dc:creator>CyrusN</dc:creator><description>Joc: &amp;quot;In static constructors you could disallow access to static non-nullable fields (as well as static non-nullable properties, functions with static non-nullable return types or non-nullable out and ref parameters) of other types: e.g. inside the static constructor of Cyrus, the static fields of Person would not be accessible.&amp;quot;&lt;br&gt;&lt;br&gt;If you disallow all that... then how do you initialize your static fields? :)&lt;br&gt;&lt;br&gt;You're basically disallowing me from touching any type that might transitively touch a type with a static initializer.  This will be very constraining.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412709</link><pubDate>Wed, 27 Apr 2005 22:41:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412709</guid><dc:creator>CyrusN</dc:creator><description>Joe: &amp;quot;Null of T should never be considered to be a real T. That's ludicrous--it isn't true, first of all...&amp;quot;&lt;br&gt;&lt;br&gt;Well... i compeltely agree :)&lt;br&gt;&lt;br&gt;Sorry if i ever let you think otherwise.  However, non-null of T should be considered to be a real T.&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;quot;I think type systems should not be in the business of lying--and gives way to crap like NullReferenceExceptions.&amp;quot;&lt;br&gt;&lt;br&gt;And Some of T and nil give way to &amp;quot;MissingMatchException&amp;quot; in functional languages :)&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;quot;I prefer to fix these styles of errors in the static type system&amp;quot;&lt;br&gt;&lt;br&gt;So do i.  that's why i'd like to bring good old non-null types to C# and let people use Optional/Maybe to allow for null values.  However, i'm running into many problems trying to do that, and i thought i'd share it with you.  It sounds like we feel very similarly about this stuff, and i'm trying to get a ebtter solution so that if you *have* to use C# it doesn't end up totally pissing you off :)&lt;br&gt;&lt;br&gt; &amp;quot;than let dynamism run a good program at runtime. But nowadays, that seems to be an old skool point of view&amp;quot;&lt;br&gt;&lt;br&gt;Well... i'm trying to remove dynamism.   but it's hard when the entire system is based around it :(</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#412818</link><pubDate>Thu, 28 Apr 2005 05:34:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412818</guid><dc:creator>Joe Duffy</dc:creator><description>Cyrus, cool... think we're on the same page. Although we may differ in that I think nullability should be opt-in rather than opt-out.&lt;br&gt;&lt;br&gt;In Haskell for example, forcing people to unbox a &amp;quot;Maybe of a&amp;quot; makes them realize they have to deal with the null case, or pushes it onto the caller if it understands only an &amp;quot;a&amp;quot;... (i.e. not nullable) And with pattern matching it becomes so much simpler than doing null branch testing to avoid sporadic NullRefExceptions.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#413983</link><pubDate>Mon, 02 May 2005 18:39:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:413983</guid><dc:creator>joc</dc:creator><description>Joc: &amp;quot;In static constructors you could disallow access to static non-nullable fields (as well as static non-nullable properties, functions with static non-nullable return types or non-nullable out and ref parameters) of other types: e.g. inside the static constructor of Cyrus, the static fields of Person would not be accessible.&amp;quot; &lt;br&gt;&lt;br&gt;CyrusN: &amp;quot;If you disallow all that... then how do you initialize your static fields? :)&amp;quot;&lt;br&gt;&lt;br&gt;With your static constructors! Person's static constructor is allowed to initialize Person's static non-nullable fields and Cyrus' static constructor is allowrd to initialize Cyrus' static non-nullable fields but you may not use the former to initialize the latter and conversely.</description></item><item><title>re: Difficulties with non-nullable types (part 3)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#536753</link><pubDate>Wed, 22 Feb 2006 13:09:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:536753</guid><dc:creator>Beijing hotels</dc:creator><description>Beijing hotels</description></item><item><title>I want non-nullable types in C#4</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/26/412040.aspx#4047152</link><pubDate>Wed, 25 Jul 2007 20:26:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4047152</guid><dc:creator>Patrick Smacchia</dc:creator><description>&lt;p&gt;There is no-doubt that the C#2 nullable-types is a cool feature. However I regret that C# don&amp;amp;#39;t support&lt;/p&gt;
</description></item></channel></rss>