Proposal: Allow short circuited ternary operators. #1480
Replies: 12 comments
-
Confused individual. What's confusing...ternary operators or something I've written. I know it's only saving four characters...but my current project has several hundred of these in Razor markup. |
Beta Was this translation helpful? Give feedback.
-
Allowing omission of the colon and false expression would make conditional operators ambiguous to parse. Also, the default value of string is not an empty string. |
Beta Was this translation helpful? Give feedback.
-
Also, the default value of string is not an empty string. |
Beta Was this translation helpful? Give feedback.
-
Seems ambiguous to me. Is there any language that has this sort of short circuited ternary operator? |
Beta Was this translation helpful? Give feedback.
-
Yes although it's normally |
Beta Was this translation helpful? Give feedback.
-
The Elvis operator is quite different in that it will use the second operand if the first operand is not true, otherwise it will use the first operand. That doesn't fit the use case you outlined above in your proposal, nor would it fit in C# since it doesn't consider anything to be "truthy" other than Boolean expressions. The C# equivalent would be |
Beta Was this translation helpful? Give feedback.
-
Ok so you've now corrected two issues which aren't related to the core proposal itself, Well done aren't you clever! Any comment on what I actually proposed? Or are you here to just knock down people proposing things for the first time? |
Beta Was this translation helpful? Give feedback.
-
Clarity is important. Pretty much every proposal here goes through the ringer, including those brought up by the team members themselves. Don't take it personally. |
Beta Was this translation helpful? Give feedback.
-
@scottgal I think it's a good use case for creating your own operator overload. Just choose one that's fitting, in my example it's public static string operator &(bool condition, string valueIfTrue) => condition ? valueIfTrue : null; Of course, you'd have to define this operator for every type you'd want to use it with, but since you've mentioned it's essentially one use case repeating multiple times, I think it'd work. I just feel like it's not enough of a feature in itself, as it's pretty easily achieved by adding some code on your own :) |
Beta Was this translation helpful? Give feedback.
-
@amis92 You can't add a custom operator between two framework types. |
Beta Was this translation helpful? Give feedback.
-
Perhaps this would work just as well (assuming an appropriate @If(Model.SelectedReportIndex == file.Index, "active in") public static string If(bool condition, string trueValue) => condition ? trueValue : string.Empty; |
Beta Was this translation helpful? Give feedback.
-
Note that Also, as aready stated (and naming aside) So (just summarizing) code that would have been written like this
could be simplified to
which would expand in the compiler to
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Apologies if there's some known format I'm meant to follow. But longtime listener first-time caller etc...
In short, my proposal is a modification to ternary operators. This would be especially useful in Razor in ASP.NET also more generally.
So when you have for example
@(Model.SelectedReportIndex == file.Index ? "active in" : "")
Which is a fairly common pattern in Razor markup for conditionally adding CSS classes inline to some HTML tag. You always wind up having to specify an empty string as the 'false' condition. What I'd really want to do is:
@(Model.SelectedReportIndex == file.Index ? "active in")
Hence the ternary operator is 'short-circuited' with the second action essentially being 'void' (or really the default value for the type of the true condition).
A further example may be inline in normal C# code...again the benefit is added brevity:
IsNullOrEmpty(argumentName) ? throw new ArgumentNullException(nameof(argumentName));
Of course this would be equivalent to:
if (IsNullOrEmpty(argumentName)) throw new ArgumentNullException(nameof(argumentName));
But somewhat shorter (I fully admit it's less of a compelling case in non-razor code but still a pet peeve that I need the if() 😀
Beta Was this translation helpful? Give feedback.
All reactions