-
-
Notifications
You must be signed in to change notification settings - Fork 746
Make Nullable a template #6270
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
Make Nullable a template #6270
Conversation
|
Thanks for your pull request and interest in making D better, @marler8997! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
| For example, if `T` is a class, assigning the value to `null` does not nullify | ||
| this struct. | ||
| */ | ||
| struct NullableWithExtraField(T) |
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.
Hmm this is a new public symbol. Can't we move this into the eponymous template and thus make it private for now (and also save on template instantiations)?
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.
Lol, I just finished editing the description to explain why I think it should be "public" :) But I don't really like the name...
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.
Hmm, but what's the use case for this? Is this just to simply the deprecation period?
If we really have to do so, can't we use an additional flag like Nullable!(ExtraField.Yes, T, nullValue)? Adding new public symbols shouldn't be done lightly. Once added, we never get rid of them.
…rent implementations.
0071d94 to
ee49a4c
Compare
|
Adding a new name for small changes in behavior ... this smells like PHP. If we consider |
Agreed. However, I was thinking that this was still a valid use case, though the names are confusing. Currently, Optional!Object(null).hasValue == true
// same as
Nullable!Object(null).isNull == falseEven though these are essentially the same thing, the Maybe we should create a new type named |
Is there a reason for them not to be both named |
|
Let me summarize the proposal so far. Currently we have two Nullable template structs: // Wrap a value of type T with an extra "nullified" state by using an extra bool field
// NOTE: this extra state is added regardless of if T can already be set to null
struct Nullable(T) { ... }
// Wrap a value of type T and interpret the state as "nullified" when the value is
// set to `nullValue`
struct Nullable(T, T nullValue) { ... }The latest idea is to:
// If T has a null value then this is an alias to Nullable(T, T nullValue), otherwise,
// it is a value/boolean pair where the boolean is used to represent an extra "nullfied"
// state in place of not having a null value.
template Nullable(T)
{
static if (hasNullValue!T)
{
alias Nullable = Nullable!(T, nullValue!T);
}
else
{
struct Nullable
{
// current definition of Nullable ...
// maybe this would just use an instance of Optional
// with some wrapper methods that translate isNull to hasValue or something
}
}
}
// Wrap a value of type T and interpret the state as "nullified" when the value is
// set to `nullValue`
// NOTE: this is not changed from the current definition
struct Nullable(T, T nullValue) { ... }
// Wrap a value of type T with an extra "no value" state by using an extra bool field
// NOTE: This is basically the same as the current Nullable(T) template struct
struct Optional(T)
{
private T _value;
private bool _hasValue;
bool hasValue() const { return _hasValue; }
bool setNoValue() { _hasValue = true; }
// ...
} |
As I just posted in #625, I really don't think that the current behavior is a bug. Rather, distinguishing between a value of However, I also don't think that complicating things here by trying to create new versions of |
|
Let me reiterate; as I said on your other PR, that I don't agree with these changes. |
|
@MetaLang the plan at this point isn't to integrate this change as it exists. The plan would be to introduce a new // this is what exists today that is confusing
assert(Nullable!MyClass(null).isNull == false);
// this is the proposed alternative
assert(Optional!MyClass(null).hasValue == true);I think everyone agrees that Note that the reason for these changes are
Adding |
|
I'm interested in people's opinions of introducing the proposed |
Well, I think that I've made it fairly clear that I'm against it. Others will have to comment for themselves. |
|
Ok, @jmdavis doesn't think the introduction of Let me summarize the proposal for anyone else to chime in. The first reason for introducing This reason alone may or may not justify introducing |
|
Closing this PR and the matter of adding the |
Make Nullable a template so that it can be modified to "select" different implementations. This is in preparation for #6253
The plan is to make
Nullablean alias to eitherNullableWithExtraField!TorNullable!(T, nullValue)depending on if the type has a "sane" default null value (i.e. classes/interfaces/pointers/dynamic arrays/some enums). One reason for doing this is thatNullable!(T, nullValue)does not add an extra field, allowing values like classes/enums/pointers to only take one register.Note, I think making
NullableWithExtraFielda public struct is useful since it allows an application to force a type to have an extra "null" state even if the type has a "null" value. However, maybe someone can come up with a better name?