Proposal: Not-null conditional operator #716
Replies: 15 comments
-
One option is to use the new return a?.b is string s ? foo(s) : null; However, you are basically proposing being able to write:
which is then effectively lowered by the compiler to:
This is already covered by c# conditional operator without else. That proposal received a lot of negative feedback, including from me. However, upon reflection, I see uses for this short-hand ternary operator and I'm a fan of the idea these days. |
Beta Was this translation helpful? Give feedback.
-
Another option is to make |
Beta Was this translation helpful? Give feedback.
-
Actually, what I am proposing is more like:
which would then be lowered by the compiler to:
This would ensure that the type of the expression would always be typeof(result). i.e.
|
Beta Was this translation helpful? Give feedback.
-
No way, if we get this at all, it needs to be nullable if it's a value type just like |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Do I understand correctly, that a lot of the objections in #197 have to do with difficulty around what the concrete type is for the default(...) that is to be implicitly returned in case the condition is false? Would that problem disappear, if the behavior were, that if the condition is false, no assignment at all would take place? |
Beta Was this translation helpful? Give feedback.
-
No way, from the very beginning C# language designers made bool the only type that is supported in if conditional checks and the ternary operator. One of the effects this had was that we always know that the expression evaluates to a bool. While still evaluating to a bool, I now have to do mental gymnastics to consider this special case. This will hinder more than help, IMO. |
Beta Was this translation helpful? Give feedback.
-
The issue I see is that for the expression,
there are so many ideas of what it could be shorthand for:
and, as @HaloFour points out on that other proposal, how is the following handled?
Does the I like the idea of it being shorthand for the 2nd option, above. But I suspect the idea caries too much negative baggage for it to ever become a language feature. |
Beta Was this translation helpful? Give feedback.
-
public class C {
object M(int o) => null;
object M(string str) {
return M(str?.Length ?? return null);
}
} Rel: #176 |
Beta Was this translation helpful? Give feedback.
-
Essentially, the request here is for the complement of the null coalescing operator, e.g. return the first null value or last item provided, rather than find the first non-null value or last value provided that the null coalescing operator provides. The problem is that he proposed a single question mark as the operator to use, which would cause ambiguity. Perhaps a different operator, such as !?, ?!, or even !! would be better. Though, it wouldn't be too difficult to create an extension method which was similar to Option.map or Option.bind which would solve the same problem. |
Beta Was this translation helpful? Give feedback.
-
💭 |
Beta Was this translation helpful? Give feedback.
-
I agree, @TheJayMann Like stated in my initial post, I do consider this as a complementary null coalescing operator. I even considered the !? and !! operators myself, but came to the conclusion that the ? operator would be less cryptical and easier to understand because of its similarity to the ?: operator. I am leaning a bit more towards the !! operator now :)
or combining !! and ??
but then again, this would be easier readable as
|
Beta Was this translation helpful? Give feedback.
-
This feature make it better in somewhat long condition/expression var x = Some?.Long?.Condition()?.Items[x].Value != null ? new Some.Long.ClassName(variable) : null;
//
var x = Some?.Long?.Condition()?.Items[x].Value !? new Some.Long.ClassName(variable); I would like propose @tseemann-xematec I think we cannot use |
Beta Was this translation helpful? Give feedback.
-
I agree with @TheJayMann and @Thaina's suggestion. Using |
Beta Was this translation helpful? Give feedback.
-
Aside to @casperOne ...
Any type can be used within an if or while if that type implements the true and false operators! This is very seldom used, but I have seen good use of it. I've also (ab)used it with almost criminal intent. 😉 |
Beta Was this translation helpful? Give feedback.
-
(Or a single implicit conversion operator to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When the value of a conditional expression is dependent on calling a function with a non-null argument,
I often find myself writing the following somewhat repetitive code, using the conditional operator (?:)
return a?.b != null ? foo(a.b) : null;
Besides being a rather verbose expression, the repeated mentioning of "null" adds a lot of noise.
It would be nice if the conditional operator (?:) would allow for both null-checking and having an implicit null value, simply by omitting the false-part of the expression, so that the same could be achieved using the following simplified code:
return a?.b ? foo(a.b);
This could also be considered as an inverse null-coalesce operator (??), in that the statement
reads "if a?.b is not null, return foo(a.b), otherwise just return null"
Beta Was this translation helpful? Give feedback.
All reactions