<?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 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx</link><description>The fundamental difficulty that arises when trying to implement non-nullable types is actually enforcing that the value is not ever null. With value types, this is ensured by having a strict rule that there must exist a public no-arg constructor that</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 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411679</link><pubDate>Mon, 25 Apr 2005 14:13:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411679</guid><dc:creator>damien morton</dc:creator><description>&lt;a rel="nofollow" target="_new" href="http://research.microsoft.com/SpecSharp/"&gt;http://research.microsoft.com/SpecSharp/&lt;/a&gt;&lt;br&gt;&lt;br&gt;Spec# is an extension of C#. It extends the type system to include non-null types and checked exceptions. It provides method contracts in the form of pre- and postconditions as well as object invariants.&lt;br&gt;&lt;br&gt;Vote #1 for Spec#</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411683</link><pubDate>Mon, 25 Apr 2005 14:28:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411683</guid><dc:creator>Udi Dahan - The Software Simplist</dc:creator><description>Just a minor correction - Derived doesn't actually derive from Base.</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411791</link><pubDate>Mon, 25 Apr 2005 18:09:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411791</guid><dc:creator>Stuart Ballard</dc:creator><description>public class Foo {&lt;br&gt;  string! x;&lt;br&gt;  string! y = x;&lt;br&gt;  public Foo(string! name) {&lt;br&gt;    x = name;&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;br&gt;How on earth do you resolve that one...?</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411858</link><pubDate>Mon, 25 Apr 2005 21:54:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411858</guid><dc:creator>CyrusN</dc:creator><description>Udi: &amp;quot;Just a minor correction - Derived doesn't actually derive from Base. &amp;quot;&lt;br&gt;&lt;br&gt;Nice catch.  I've corrected it.</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411859</link><pubDate>Mon, 25 Apr 2005 21:55:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411859</guid><dc:creator>CyrusN</dc:creator><description>Damien: The problem with solutions like Spec# is that they don't necessarily solve the problems that i'm goign to be outlining here.  So you can end up with NullReferenceExceptions at runtime.  They set up a nice type system, but don't enforce that it always be maintained.&lt;br&gt;&lt;br&gt;I'd like to include non-nullable types in C#, but actually have them completely working (which will take a lot more effort).</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411862</link><pubDate>Mon, 25 Apr 2005 21:59:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411862</guid><dc:creator>CyrusN</dc:creator><description>Stuart: &amp;quot;&lt;br&gt;public class Foo { &lt;br&gt;....string! x; &lt;br&gt;....string! y = x; &lt;br&gt;....public Foo(string! name) { &lt;br&gt;........x = name; &lt;br&gt;....} &lt;br&gt;} &lt;br&gt;&lt;br&gt;How on earth do you resolve that one...? &amp;quot;&lt;br&gt;&lt;br&gt;This would not be allowed.  This can be rewritten as:&lt;br&gt;&lt;br&gt;public Foo(string! name) {&lt;br&gt;     y = x;&lt;br&gt;     x = name;&lt;br&gt;}&lt;br&gt;&lt;br&gt;And, as we can see, &amp;quot;x&amp;quot; is used before it is definitely assigned to with a valid value.  This is the same as if you had written:&lt;br&gt;&lt;br&gt;public void Bar() {&lt;br&gt;....string x;&lt;br&gt;....string y = x;&lt;br&gt;}&lt;br&gt;&lt;br&gt;then this is just not allowed.&lt;br&gt;&lt;br&gt;Does that make sense?</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411884</link><pubDate>Mon, 25 Apr 2005 22:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411884</guid><dc:creator>Stuart Ballard</dc:creator><description>It makes perfect sense, but it's inconsistent with the behavior of all other types in the language as it stands.&lt;br&gt;&lt;br&gt;class Foo {&lt;br&gt;  string x = y;&lt;br&gt;  string y = x;&lt;br&gt;}&lt;br&gt;&lt;br&gt;This is completely legal and sets x and y both to null.&lt;br&gt;&lt;br&gt;It's not how I'd have designed the language in the first place, but it's how the language stands today, and naturally you don't want to break existing legal code, no matter how strange it is to write code that relies on such an obscure (mis)feature of initialization order.&lt;br&gt;&lt;br&gt;So IMO it's a little hard to justify why that should be permitted but it should be illegal if the strings turn into string!s.&lt;br&gt;&lt;br&gt;See my last few comments on the SDR post for more such scenarios and thoughts on this kind of issue.</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#411922</link><pubDate>Tue, 26 Apr 2005 00:01:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:411922</guid><dc:creator>CyrusN</dc:creator><description>Stuart: &amp;quot;It's not how I'd have designed the language in the first place, but it's how the language stands today, and naturally you don't want to break existing legal code, no matter how strange it is to write code that relies on such an obscure (mis)feature of initialization order. &amp;quot;&lt;br&gt;&lt;br&gt;This wouldn't be breaking any existing legal code. :)&lt;br&gt;&lt;br&gt;&amp;quot;So IMO it's a little hard to justify why that should be permitted but it should be illegal if the strings turn into string!s. &amp;quot;&lt;br&gt;&lt;br&gt;Not really.  The above is permitted because we have guaranteed that all constraints have been met before the use of the variables.  Now, the ! just says that it needs to be initialized to something that isn't null before being used.  This is identical to how locals must be proven to initialized before they're used.&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;quot;See my last few comments on the SDR post for more such scenarios and thoughts on this kind of issue.&amp;quot;&lt;br&gt;&lt;br&gt;Sure!</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#412101</link><pubDate>Tue, 26 Apr 2005 14:46:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412101</guid><dc:creator>DrPizza</dc:creator><description>Personally, I've always found C# (and Java's) decision to construct classes more or less backwards to be rather weird.&lt;br&gt;&lt;br&gt;In C++, there's no problem here.  Derived::Derived() isn't called until Base::Base() has finished; this means that Derived's members aren't initialized until Base is done constructing.  Virtual calls within Base::Base() call Base's version of the method, not the overridden one, because at the time of the call, you don't yet have a Derived (so can't call its methods), only a Base.&lt;br&gt;&lt;br&gt;It also uses init lists to initialize its members, in preference to normal-looking (but actually special, under your scheme) initializations in the constructor body.&lt;br&gt;&lt;br&gt;Even without calling base class constructors prior to derived class initialization, init lists would seem a good way of handling the situation.&lt;br&gt;&lt;br&gt;Under such a system one would have something like:&lt;br&gt;&lt;br&gt;public abstract class Base&lt;br&gt;{&lt;br&gt;	public Base()&lt;br&gt;	{&lt;br&gt;		Setup();&lt;br&gt;	}&lt;br&gt;	public abstract void Setup();&lt;br&gt;}&lt;br&gt;public class Derived : Base&lt;br&gt;{&lt;br&gt;	string! name;&lt;br&gt;	public Derived(string! name_) : name(name_), base()&lt;br&gt;	{&lt;br&gt;	}&lt;br&gt;	public override void Setup()&lt;br&gt;	{&lt;br&gt;		Console.WriteLine(&amp;quot;Debugging Info: &amp;quot; + name.ToString());&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;Should be simple enough to diagnose (if it's not in the init list, it's not initialized, so either null-initialize (nullable) or compile-time error (non-nullable)), shouldn't require any special magic (&amp;quot;if it's in the constructor's body but before the base class call it has limited access to this), should extend existing constructs (C#'s rudimentary init list) instead of requiring new ones (explicit base constructor calls), should do the Right Thing, and shouldn't break any existing code.</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#412102</link><pubDate>Tue, 26 Apr 2005 14:58:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412102</guid><dc:creator>DrPizza</dc:creator><description>(in addition to the rule that you can't access 'this' until all members are initialized)</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#412222</link><pubDate>Tue, 26 Apr 2005 20:38:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412222</guid><dc:creator>CyrusN</dc:creator><description>DrPizza: Absolutely right.  I had meant to make that part of my post, but totally forgot about it.  I've added a small edit at the bottom to reflect this.</description></item><item><title>re: Difficulties with non-nullable types (part 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#412344</link><pubDate>Wed, 27 Apr 2005 02:44:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412344</guid><dc:creator>DrPizza</dc:creator><description>&amp;quot;One thing I don't like is how C++ initialization lists look.  I'd like to come up with a nicer looking syntax for that.&amp;quot;&lt;br&gt;&lt;br&gt;Well, you guys already copied them for base class constructor calls.  Personally, I like distinguishing between assignment and initialization.  It angers me that in C++ you can write:&lt;br&gt;&lt;br&gt;Foo x = y;&lt;br&gt;&lt;br&gt;and rather than calling Foo::operator=(y) (which is, let's face it, what it looks like) you're actually calling Foo::Foo(y).  So I always use initializers anyway (that is, I'd write Foo x(y);).  Since init lists are consistent with this syntax, they don't seem too problematic to me.&lt;br&gt;&lt;br&gt;But... yeah.  I've never really understood the rationale behind the backwards construction.  I guess C# and Java treat the object as a finished object _prior_ to calling the constructor, whereas in C++ the constructor really _does_ &amp;quot;construct&amp;quot;; until Foo::Foo is completed *it's not yet a Foo*.  Whereas in C# and Java, it's a Foo straight away, and the constructor is more of an &amp;quot;initializer&amp;quot; than a &amp;quot;constructor&amp;quot;.  I haven't really explained that very well.</description></item><item><title>Difficulties with non-nullable types (part 4)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#412445</link><pubDate>Wed, 27 Apr 2005 09:36:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:412445</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 2)</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#413508</link><pubDate>Fri, 29 Apr 2005 20:00:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:413508</guid><dc:creator>Luke</dc:creator><description>So, the fundamental problem is that while value types can be initialized to a default non-null value, there is no suitable non-null value to use for reference types. It becomes particularly apparent when you consider abstract reference types like interfaces. So, the discussion has been about somehow ensuring that before being used, non-nullable reference types are initialized to a non-null value, by other code, since initialization cannot be done automatically. &lt;br&gt;&lt;br&gt;Let me throw out another idea totally distinct from this. Take a lesson from the way structs solve the problem. Say we allow non-nullable types to be null, which is their initial value, but the behavior of a null value is different. If _foo is null and you do _foo.Bar, instead of immediately getting a null reference exception, the call goes through with a null &amp;quot;this&amp;quot; pointer. Perhaps a type would specify static methods or a concrete derivative type to be called when &amp;quot;this&amp;quot; is null. &lt;br&gt;&lt;br&gt;Then you could say:&lt;br&gt;string foo = null;&lt;br&gt;int x = foo.Length; // throws&lt;br&gt;string! bar = null;&lt;br&gt;int y = bar.Length; // y = 0&lt;br&gt;&lt;br&gt;So, instead of running away from the null we always begin at, we simply make null not be null anymore. &lt;br&gt;&lt;br&gt;I haven't throught this all the way through, but it's an interesting idea.</description></item><item><title>I want non-nullable types in C#4</title><link>http://blogs.msdn.com/cyrusn/archive/2005/04/25/411630.aspx#4047151</link><pubDate>Wed, 25 Jul 2007 20:26:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4047151</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>