-
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
Cast to Nullable operator #555
Comments
IMO, in the ?: operator the cast should not be needed at all. |
This is already being taken care of with #33. |
@erik-kallen I agree, like |
@lachbaer It does not. I didn't see any reason for |
@jnm2 The reason is to use a cast expression rather than |
@lachbaer If the idea is to frame it as a cast to |
The idea here is to have that |
Perhaps. #36 |
@jnm2 And what do you think about |
One syntax that has been unofficially proposed is a post-fix "damnit" operator string? name = GetName();
int length = name!.Length; // suppresses the possible nullable reference warning |
@HaloFour That's rather a |
@lachbaer I prefer |
@lachbaer No, it's not a |
The member access isn't a part of the operator. All the void PrintName(string name) Console.WriteLine(name);
string? name = GetName();
PrintName(name!);
string definitelyNotNull = name!; For nullable value types it could do the same thing by effectively just compiling down to a call to the |
@jnm2 But those are "casts" that have a following operation, no "pure" casts |
Converting from a non-nullable type to a nullable type is a widening conversion. There's no need for an explicit cast. int i = 123;
int? j = i; // perfectly legal The only real exception here is when it comes to conditional ternary operations as currently the compiler is unable to determine a common type between As for the inversion, it certainly makes sense to require some kind of cast operation. I think that the "damnit" operator covers those bases. I don't see it included in #36 or the current version of the proposal, so I might open a separate proposal specifically for it. |
Yeah, that's where I encountered it, too. Then there are probably no other occurances of the problem, otherwise they would already have been reported. About the widening, I sometimes explicitly cast the nullables, simply because I like it more verbose in some places.
Go ahead 😃 |
Language features need to provide huge value to warrant inclusion. Today you have the following options already: int value = posvalue.Value;
int value = (int)posvalue;
// and also if you don't want to throw on null
int value = posvalue.GetValueOrDefault(); We don't need a third way to do the exact same thing. Our language is not trying to be one where there are 3+ synonyms for non-common stuff. It just does not provide enough value in our eyes. |
Definite statement. Closed. |
Syntax Sugar:
When there is the need for an explicit cast to a nullable the nullable type of the operand must be explicitly given.
The type of the cast can however be infered by the compiler, so that the cast can be shortend.
The same for back casts, but with the
!
character.This is no definite proposal. Just a discussion whether you like it or not.
The text was updated successfully, but these errors were encountered: