c# conditional operator without else #197
Replies: 26 comments
-
What is the result for such an expression supposed to be? |
Beta Was this translation helpful? Give feedback.
-
Like @HaloFour, I'm confused as to what would happen when the expression is false. It can't be unassigned, as the compiler wouldn't then let you use string returnValue = someBool ? "this is a return value"; would be the equivalent of: string returnValue = someBool ? "this is a return value" : null; This can be generalised to: T value = someBool ? <some T value> : default(T); But, in your example, you want it to be equivalent to Sorry, but I don't think this is a good suggestion. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour @DavidArno Thank you for answering. I have updated my issue. Sorry for being so complicated :( I think its a great idea how @DavidArno has generalised this. He is understanding me better then myself :)
|
Beta Was this translation helpful? Give feedback.
-
I think the value should be default(T) or the old value. For example, instead of writing: var sample = "foo";
var statement = true;
if (statement)
{
sample = "bar";
} or even (With a useless var sample = "foo";
var statement = true;
sample = statement ? "bar" : sample; I would love to be able to write: var sample = "foo";
var statement = true;
sample = statement ? "bar"; It would be a really sweet sugar 👍 |
Beta Was this translation helpful? Give feedback.
-
@aloisdg I understand you, but one question. How would you handle something like this (calling the operator in a parameter): Should be |
Beta Was this translation helpful? Give feedback.
-
@NiklasRaab Indeed. I think |
Beta Was this translation helpful? Give feedback.
-
@aloisdg so, var sample = "foo";
var statement = false;
sample = statement ? "bar"; should left var sample = "foo";
var statement = false;
sample = String.Intern(statement ? "bar"); will make |
Beta Was this translation helpful? Give feedback.
-
@ig-sinicyn Why is it scary? Edit:
|
Beta Was this translation helpful? Give feedback.
-
Oh, thanks. Now it's much better. |
Beta Was this translation helpful? Give feedback.
-
@ig-sinicyn sarcasm, isnt it? I am not sure. In your case why wouldn't you use a classic ternary operatyor: |
Beta Was this translation helpful? Give feedback.
-
Expressions and assignments are two different things. The expression |
Beta Was this translation helpful? Give feedback.
-
@HaloFour return As said before, in your code, just use a ternary operator. To use @ig-sinicyn example: using System;
var sample = "foo";
var statement = false;
sample = string.Intern(statement ? "bar" : sample);
Console.WriteLine(string.IsNullOrEmpty(sample) ? "empty" : sample) or why not using System;
var sample = "foo";
var statement = false;
sample = statement ? "bar";
Console.WriteLine(string.IsNullOrEmpty(sample) ? "empty" : string.Intern(sample)); |
Beta Was this translation helpful? Give feedback.
-
Because I can. As far as I can see it is same motivation that inspired this proposal as well as many others, #196, #183, #170 to name a few. I do not think that ability to write if not !(?!x?.Y?z?a??a2???a3??!a4!) ... is a good addition to c#. We have an APL for this : ) |
Beta Was this translation helpful? Give feedback.
-
By the way, I dont say that it is feasible. It may not. I dont know. I see OP idea and I see myself using an operator like this one. |
Beta Was this translation helpful? Give feedback.
-
@ig-sinicyn It is called Syntactic sugar. For example the Is this feature helpful? Why someone would want to use it? What are we looking to resolve? I see OP problem. I see an idea of a solution. (Maybe not a working one 😄 ) |
Beta Was this translation helpful? Give feedback.
-
I see you point but I'd prefer to have c# team being focused on a real issues instead of reinventing one more way to write the if() statement :) Ok, there is half a dozen ways to declare a property and more is coming. Should all other parts of a language to be doomed in a same manner? |
Beta Was this translation helpful? Give feedback.
-
@ig-sinicyn Sure. I agree this one is not a priority, but I don't think it is a bad one. The language is alive and it is nice. Nobody forces anyone to use |
Beta Was this translation helpful? Give feedback.
-
You have taken a proposal of questionable worth and turned it into one that fundamentally changes the way the compiler would have to work in order to achieve it. For: var sample = "foo";
var statement = false;
sample = statement ? "bar"; You are asking for the compiler to process the expression, var sample = "foo";
var statement = false;
var newValue = F(statement ? "bar"); Then it must detect that the expression isn't directly assigned and so it then has to turn the last line into The compiler doesn't work like that, at all, currently. It never considers what is going to happen to an expression when working out how to compile that expression. This would be a significant deviation away from how the compiler currently works. And it would be pointless anyway. There already exists a better way of handling such assignments: do not reassign :- var statement = false;
var sample = statement ? "foo" : "bar"; If you can't use that, then there is a clear indication that your code is too complex and needs rationalising. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno Ok. So lot of works for almost no benefits. Get it :) |
Beta Was this translation helpful? Give feedback.
-
@DavidArno @aloisdg @ig-sinicyn But back to the beginning:
This should not be complicated to implement and sometimes it would be a nice feature.
|
Beta Was this translation helpful? Give feedback.
-
I believe that the syntax itself would also be ambiguous. You can currently nest ternary conditional operators and the compiler can parse those expressions since every |
Beta Was this translation helpful? Give feedback.
-
Maybe i'm wrong but I feel like it useless if it's only to put default(T) on the varible when false. Because i don't see the problem to add ": null" to your stament. But if it should keep the old value that better because you sometime can't do (and other time don't want to do) (myVar = myBool ? other : myVar). The form with if(){} can use 7 More line
Or if you agree whith no bracket (+3 line)
|
Beta Was this translation helpful? Give feedback.
-
I found myself wanting this in MVC, something like this seems like it would be pretty convenient: Also, if you wanted to invert this you could do @HaloFour if no matching |
Beta Was this translation helpful? Give feedback.
-
Just wanted to chime in on a little old discussion to share another possible use case that for a feature like this, but if not feasible is understandable. Currently, if you are assigning a value by attempting to parse if from a string, you can do it with one line:
With this kind of feature, would it be possible to shorthand this further? Like so? Therefore it would only assign 42 if the condition from the TryParse is false, but may be tricky with the other scenario where the condition is not met in an assignment. Very minor reduction of redundant code, but thought I'd add this scenario to the discussion anyway. In fact, this may have ended up as more of a proposal to enhance TryParse to assign a value of type T if the parse fails. |
Beta Was this translation helpful? Give feedback.
-
I like the general idea of simplifying the
Example:
|
Beta Was this translation helpful? Give feedback.
-
Looking for it as
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Please provide support for conditional operator without an else. For example:
string returnValue = someBool ? "this is a return value"
At the moment you have to write:
string returnValue = someBool ? "this is a return value" : null
or
string returnValue = someBool ? "this is a return value" : default(string)
Thank you for the general statement blow:
T value = someBool ? <some T value> : default(T);
It would be small but nice 👍
Beta Was this translation helpful? Give feedback.
All reactions