Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog/nullable.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
`std.typecons.Nullable` will return true for `isNull` on objects initialized with null

The following behavior for nullable is changed:
---
Nullable!Object n = null;
assert(!n.isNull); // old behavior
assert(n.isNull); // new behavior
---
This applies to classes, interfaces, pointers and dynamic arrays.
131 changes: 116 additions & 15 deletions std/typecons.d
Original file line number Diff line number Diff line change
Expand Up @@ -2335,8 +2335,28 @@ Practically $(D Nullable!T) stores a $(D T) and a $(D bool).
*/
struct Nullable(T)
{
static if (is(T == enum))
{
static if (OriginalType!T.min < T.min) private enum nullValue = cast(T) OriginalType!T.min;
else static if (OriginalType!T.max > T.max) private enum nullValue = cast(T) OriginalType!T.max;
else private enum nullValue = false;
}
else static if (is(T == class) || is(T == interface) || isPointer!T || isDynamicArray!T)
{
private enum nullValue = cast(T) null;
}
else
{
private enum nullValue = false;
}
private enum hasNullValue = !is(typeof(nullValue) == bool);

static if (hasNullValue)
{
private T _value = nullValue;
}
// simple case: type is freely constructable
static if (__traits(compiles, { T _value; }))
else static if (__traits(compiles, { T _value; }))
{
private T _value;
}
Expand All @@ -2354,7 +2374,10 @@ struct Nullable(T)
);
}

private bool _isNull = true;
static if (!hasNullValue)
{
private bool _isNull = true;
}

/**
Constructor initializing $(D this) with $(D value).
Expand All @@ -2365,7 +2388,8 @@ Params:
this(inout T value) inout
{
_value = value;
_isNull = false;
static if (!hasNullValue)
_isNull = false;
}

/**
Expand All @@ -2375,18 +2399,23 @@ Params:
*/
bool opEquals()(auto ref const(typeof(this)) rhs) const
{
if (_isNull)
return rhs._isNull;
if (rhs._isNull)
return false;
static if (!hasNullValue)
{
if (_isNull)
return rhs._isNull;
if (rhs._isNull)
return false;
}
return _value == rhs._value;
}

/// Ditto
bool opEquals(U)(auto ref const(U) rhs) const
if (is(typeof(this.get == rhs)))
{
return _isNull ? false : rhs == _value;
static if (!hasNullValue)
if (_isNull) return false;
return rhs == _value;
}

///
Expand Down Expand Up @@ -2512,7 +2541,10 @@ Returns:
*/
@property bool isNull() const @safe pure nothrow
{
return _isNull;
static if (hasNullValue)
return _value is nullValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work in the enum case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It passes the unittests...

else
return _isNull;
}

///
Expand Down Expand Up @@ -2542,11 +2574,13 @@ Forces $(D this) to the null state.
*/
void nullify()()
{
static if (is(T == class) || is(T == interface))
_value = null;
static if (hasNullValue)
_value = nullValue;
else
{
.destroy(_value);
_isNull = true;
_isNull = true;
}
}

///
Expand All @@ -2569,7 +2603,8 @@ Params:
void opAssign()(T value)
{
_value = value;
_isNull = false;
static if (!hasNullValue)
_isNull = false;
}

/**
Expand All @@ -2587,9 +2622,9 @@ Params:
Nullable!(int*) npi;
assert(npi.isNull);

//Passes?!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol :)

//Passes
npi = null;
assert(!npi.isNull);
assert(npi.isNull);
}

/**
Expand Down Expand Up @@ -3706,7 +3741,73 @@ auto nullableRef(T)(T* t)
NullableRef!TestToString ntts = &tts;
assert(ntts.to!string() == "2.5");
}
@safe unittest
{
enum Foo {a, b, c}
static assert(Nullable!Foo.sizeof == Foo.sizeof);

{
auto foo = nullable(Foo.a);
static assert(foo.sizeof == Foo.sizeof);
assert(!foo.isNull);
assert(foo == Foo.a);
assert(foo.get == Foo.a);
foo = Foo.b;
assert(!foo.isNull);
assert(foo == Foo.b);
foo.nullify();
assert(foo.isNull);
}
{
auto foo = Nullable!Foo.init;
static assert(foo.sizeof == Foo.sizeof);
assert(foo.isNull);
foo = Foo.c;
assert(!foo.isNull);
assert(foo == Foo.c);
}

enum Foo2 : int {a = int.min}
static assert(Nullable!Foo2.sizeof == Foo2.sizeof);

enum Foo3 : int {a = int.min, b = int.max}
static assert(Nullable!Foo3.sizeof != Foo3.sizeof);
}
@safe unittest
{
class FooClass { }
static assert(Nullable!FooClass.sizeof == FooClass.sizeof);
class FooInterface { }
static assert(Nullable!FooInterface.sizeof == FooInterface.sizeof);

Nullable!FooClass c = null;
assert(c.isNull);

Nullable!FooInterface i = null;
assert(i.isNull);
}
@safe unittest
{
auto b = Nullable!bool.init;
assert(b.isNull);
b = false;
assert(!b.isNull);
assert(b.get == false);
b = true;
assert(!b.isNull);
assert(b.get == true);
b.nullify();
assert(b.isNull);
assert(nullable(true) != b);
}
@safe unittest
{
Nullable!(int*) ip = null;
assert(ip.isNull);

Nullable!(int[]) ia = null;
assert(ia.isNull);
}

/**
$(D BlackHole!Base) is a subclass of $(D Base) which automatically implements
Expand Down