is it bug?

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 25 19:58:19 UTC 2017


On 9/25/17 2:03 PM, drug wrote:
> https://run.dlang.io/is/pZwsoX
> 
> As I can see std.format.formatElement thinks Nullable!Foo is not null, 
> try to get its value but it is null so fail?

OK, so I was confused quite a bit.

There are 2 problems. One is a bug, one is not.

First up, the non-bug:

You wrote:

writeln(bar.foo.isNull); // false
writeln(bar.foo); // error

But bar.foo is this:

struct Foo
{
    Nullable!(char[2]) value;
}

And bar.foo is nullable itself. It's a nullable!Foo.

It's a Nullable!Foo, that is not null, but contains a Nullable!(char[2]) 
that *is* null.

So when you print Foo, it calls the get on it, which works, and then 
tries to print all its members (the default thing for std.format), and 
when it gets to the *value* member, it crashes.

So bar.foo.isNull has no bearing on the actual error. 
bar.foo.value.isNull is true, and there's where the error happens.

Your first example was more confusing because it wasn't named `value`, 
it was named `foo`!

Second is the actual bug, and we don't need Bar to show it:

Foo foo;
writeln(foo.value); // Nullable.null
writeln(foo); // error

There is something different that happens when you try to print a 
Nullable directly, than when you print a struct containing the Nullable.

So this is the reduced case. The second line should print 
Foo(Nullable.null), not throw an error. And yes, it has something to do 
with the fact that it's a char[2]. int works. char[] also fails.

-Steve


More information about the Digitalmars-d-learn mailing list