-
-
Notifications
You must be signed in to change notification settings - Fork 747
Remove extra field from Nullable when unnecessary #6253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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). | ||
|
|
@@ -2365,7 +2388,8 @@ Params: | |
| this(inout T value) inout | ||
| { | ||
| _value = value; | ||
| _isNull = false; | ||
| static if (!hasNullValue) | ||
| _isNull = false; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| /// | ||
|
|
@@ -2512,7 +2541,10 @@ Returns: | |
| */ | ||
| @property bool isNull() const @safe pure nothrow | ||
| { | ||
| return _isNull; | ||
| static if (hasNullValue) | ||
| return _value is nullValue; | ||
| else | ||
| return _isNull; | ||
| } | ||
|
|
||
| /// | ||
|
|
@@ -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; | ||
| } | ||
| } | ||
|
|
||
| /// | ||
|
|
@@ -2569,7 +2603,8 @@ Params: | |
| void opAssign()(T value) | ||
| { | ||
| _value = value; | ||
| _isNull = false; | ||
| static if (!hasNullValue) | ||
| _isNull = false; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -2587,9 +2622,9 @@ Params: | |
| Nullable!(int*) npi; | ||
| assert(npi.isNull); | ||
|
|
||
| //Passes?! | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol :) |
||
| //Passes | ||
| npi = null; | ||
| assert(!npi.isNull); | ||
| assert(npi.isNull); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
enumcase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It passes the unittests...