-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposal: Allow null-coalescing ??
operator on value types
#328
Comments
This will break the following existing code:
With the existing compiler, |
@YaakovDavis To provoke y == 1 do... int? x = 0;
var y = x.GetValueOrDefault() ?? 1; PS: The |
Well, you were the one to suggest treating default values as Here's another example, more reminiscent of the one you provided:
With the existing compiler, no exception will be thrown. With the new proposal, an exception will occur. |
@YaakovDavis I think there is a misunderstanding here. By now |
You seem to suggest that will yield default results ( I'll dislike this idea very much. This will lead IMO to subtle bugs which are hard to track down. |
@YaakovDavis Just in the case of Nullables. For (build-in) standard value types there is currently no such an coalescing operator. Though for Nullables there might indeed be the ambiguity that you want to check against |
Extending |
@YaakovDavis @jnm2 For reference types the See... no difference! It would just extend to: I don't see any conflicts, only benefits (except for the ambiguity with Nullables). Extended first post for clarification |
@lachbaer If it was overwhelmingly common for people to write |
I'm with @jnm2 on this. This doesn't seem like a common pattern at all to be trying to specialize. |
I just experienced a conflict 😢 object obj = new object();
var z = obj?.GetHashCode() ?? 12345; On first sight I would say, that it should give a compiler error, because With this proposal it is arbitrary whether Restricting it to |
That is not how the language works. You used the
There is no type-safety violation here. |
As such, the type of "obj?.GetHashCode()" is int? not int
Yes, but that is not "obvious", just by seeing the GetHashCode signature.
So in this context there is no (internal) conflict, besides the already mentioned Nullable ambiguity.
Maybe you're right that there is no broad need for it. I just had some code where that idea plopped out of my head instantaneously.
Am 24. März 2017 15:31:29 MEZ schrieb CyrusNajmabadi <[email protected]>:
…> On first sight I would say, that it should give a compiler error
That is not how the language works. You used the ?. operator. As
such, the type of "obj?.GetHashCode()" is ```int?``` not ```int```.
```??``` works as expected on an ```int?``` on the left and an
```int``` on the right.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#328 (comment)
|
The |
@jnm2 @CyrusNajmabadi Also I have inspected the Roslyn code for the Null-Coalescing operator. Changing it doesn't seem to be too hard.
I'm on it, whether it will be incorporated or not :-) |
I have completed the adaptation and tested with all current kind of types. @CyrusNajmabadi |
After I have implemented the new behaviour for class C1
{
public int x {get; set;}
public int? y {get; set;}
}
var x1 = c?.x ?? 42;
var x2 = c?.y ?? 42; the behaviour of a default-coalescing-operator is completely arbitrary. Let's assume Shall For Update: |
After playing a bit with my implementation of this (dotnet/roslyn#18526) I must admit that I don't like it that much anymore.
Nevertheless it was good to have it implemented. It helped me by analyzing the usefullness of this proposal. And it was fun learning about the compiler internals during the work :-) |
I still have the idea in mind to make the null-conditional and null-coalescing operator available to custom value types or by extension-methoding existing ones, e.g. by introducing an |
Redesignating the null-coalescing
??
operator to default-coalescing operator would allow to use it on value types as well.For build in standard value types this would work like a charm:
In this example a fraction is always valid, even if not initialized.
It is a shortcut for
By comparing with
default(T)
, the null-calescing character stays intact but also allows for value types. Nullable value types have a default ofnull
, if one wants to compare the value to0
orfalse
, she must useValueType?.GetValueOrDefault() ?? ...
.Known issue
Though it would work fine with Nullables, there is the ambiguity that one wants to check against the value, but accidentially checks again
null
.Is it by purpose to check against
x != null
or is the actual intention to check againstx != 0
? Latter would have been correct with(This issue is a bit related to #196).
The text was updated successfully, but these errors were encountered: