I ran into this blog post today: https://blogs.msdn.microsoft.com/dotnet/2017/11/15/nullable-reference-types-in-csharp/

It peeked my interested, because when I first started studying D, the lack of any warning or error for this trivial case surprised me.

// Example A
class Test
{
    int Value;
}

void main(string[] args)
{
    Test t;
    t.Value++;  // No compiler error, or warning.  Runtime error!
}
https://run.dlang.io/is/naTgHC

In C#, you get a compiler error.

// Example B
class Test
{
    public int Value;
}
                                        
public class Program
{
    public static void Main()
    {
        Test t;
        t.Value++;  // Error: Use of unassigned local variable 't'
    }
}
https://dotnetfiddle.net/8diEiG

But, it's not perfect:

// Example C
class Test
{
    private static Test _instance;
    public static Test Instance
    {
        get { return _instance; }
    }

    public int Value;
}
                                        
public class Program
{       
    public static void Main()
    {
        Test t = Test.Instance;
t.Value++; // No compiler error, or warning. Runtime error!
    }
}
https://dotnetfiddle.net/GEv2fh

With Microsoft's proposed change, the compiler will emit a warning for Example C. If you want to opt out of the warning, you'll need to declare `_instance` as `Test? _instance` (see the '?' there).

Notice that that is a "breaking" change for C#, but Microsoft still considers it an improvement to the language worth pursuing. Is there hope for D, too?

Mike

Reply via email to