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
Re-enable std.typecons.Nullable and NullableRef #153
Conversation
@@ -1109,8 +1109,6 @@ unittest | |||
assert(y == 5); | |||
} | |||
|
|||
/+ | |||
|
|||
/** | |||
Defines a value paired with a distinctive "null" state that denotes | |||
the absence of a valud value. If default constructed, a $(D |
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.
If you're messing with this, you might as well fix the spelling in the description.
It looks fine from what I can see, but honestly, I don't quite get the point of |
@jmdavis: Mainly to add an invalid value to types which doesn't have it, and can be connected back to the original type. For example, I use this as |
But pointers do that. You'd just use |
But that would imply using pointers in the first place; this might not be desirable for a variety of safety reasons, or for API simplicity. Also, Nullable explicitly communicates that the value may or may not be there; a pointer doesn't necessarily do this unless documented. |
Now, the fact that it's putting stuff on the stack could be valuable. It helps avoid heap allocations and could allow for the deterministic destruction of structs which would otherwise be on the heap (though hopefully the fact that structs' destructors aren't called if they're on the heap is only a temporary implementation issue rather than a permanent issue in the language). So, this may very well be of value for performance purposes, but I don't think that it adds anything over pointers if all you're looking for is the ability to make a type nullable. It would be nice to hear more opinions on the value or lack thereof of |
I guess |
What I mean is, a Nullable!T clearly says "this value can correctly be null", while a pointer doesn't say whether or not it's allowed to be null, unless documented or assert'd or whatever. |
@XTZGZoRex And my point was that the there is no difference between So, as far as nullability goes and what you can assume about it, I see zero difference between |
Some very good points in favor of |
Though actually, pointers can be used in SafeD. It's pointer arithmetic which can't be, so that wouldn't affect this use case. The escaping referenc issue would though. |
One difference of the nullable type over pointers outside of D is that they have a special equality semantic:
This is seen in .NET framework's However, Andrei's original code doesn't have this, e.g. void main() {
Nullable!int a;
auto b = Nullable!int(4);
assert(a != b); // object.Exception@/usr/include/phobos/std/typecons.d(1177): Enforcement failed
} Does this worth the addition if this special (There are also other methods like C#'s |
All things considered, this probably is worth adding to Phobos / uncommenting out. If nothing else, the fact that it's on the stack will be of definite value to those trying to avoid the heap as much as possible. As for |
Yah, I, too, think this is worth adding. The difference between Nullable!T and T* is that the former encapsulates an optional value whereas the latter is a proxy for an optional value that exists elsewhere. The distinction may not seem too strong considering that "new T" is a limitless factory of Ts, but when we consider the performance implications this is worth having. |
Re-enable std.typecons.Nullable and NullableRef
Re-enable these types which were disabled in commit d218c12 2 years ago.
These changes were made:
Nullable!T.nullify
callsobject.clear
instead of the destructor directly.@property
andconst
annotations toisNull
andget
.