Extend the support for throw expressions #1761
Replies: 17 comments
-
Seems your beef is with conditional operators, not The problem with this is that the conditional expression is I also believe that allowing the |
Beta Was this translation helpful? Give feedback.
-
What does this significantly offer over: return SomeOperation(args) is var result && result != 0 ? result : throw new InvalidOperationException(“Something bad happened.”); ? |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h Readability. |
Beta Was this translation helpful? Give feedback.
-
Possibly but the overall intent is to be able to include a throw expression when the expression following a return keyword meets certain conditions.
Well your examples fall apart because they do not include the suggested |
Beta Was this translation helpful? Give feedback.
-
@Korporal
Which is fine,
|
Beta Was this translation helpful? Give feedback.
-
Another option coming soon (sharplab): return SomeOperation(args) switch
{
0 => throw new InvalidOperationException("Something bad happened"),
var result => result
}; |
Beta Was this translation helpful? Give feedback.
-
@Korporal You're probably going to be happiest with something like this: return SomeOperation(args).NullIf(0) ?? throw new InvalidOperationException("Something bad happened"); public static class ValueTypeExtensions
{
public static T? NullIf<T>(this T value, T comparison) where T : struct
{
if (EqualityComparer<T>.Default.Equals(value, comparison)) return null;
return value;
}
} |
Beta Was this translation helpful? Give feedback.
-
@jnm2 - That's not a bad idea, as soon as I coded it it was screaming to be refactored like this:
where
|
Beta Was this translation helpful? Give feedback.
-
@jnm2 - Which upon further pondering becomes:
where
|
Beta Was this translation helpful? Give feedback.
-
@Korporal I avoid letting library methods throw provided exceptions like that. Mainly because the exception helper filters them as coming from another assembly, but also because it's another thing to document. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 - Sure, there are always considerations (but what is "exception helper"?) however the name "ThrowIf" more or less conveys what this does without any need for documentation surely? |
Beta Was this translation helpful? Give feedback.
-
In fact this suggest (now that we have gone down this road) a new keyword |
Beta Was this translation helpful? Give feedback.
-
My original example was:
With a new keyword
or something, in fact we could retain the existing keyword but have this alternative form:
Doc: The expression |
Beta Was this translation helpful? Give feedback.
-
@Korporal The VS exception helper is this thing (note the assembly name filter): If you like it, go for it. I'd also recommend lazily creating the exception object if you do that. |
Beta Was this translation helpful? Give feedback.
-
If we leverage the new "using"
which improves readability, we can have this:
where
|
Beta Was this translation helpful? Give feedback.
-
It seems like it might fit better by using proper boolean logic, maybe? How about this: return SomeOperation(args) == 0 || throw new InvalidOperationException("Something bad happened"); This approach has a few benefits over the original suggestion: throw expressions remain pretty much the same, just relaxed; ternary syntax doesn't change; no new keywords introduced; and it's readable if you read return IsFailure(args) && throw new InvalidOperationException("Something bad happened"); TBH, I probably wouldn't use this very much, but I wanted to constructively throw an idea out there. |
Beta Was this translation helpful? Give feedback.
-
That actually seems reasonable to me as you could imagine that all throw expressions can have their type inferred by context. So, "throw ..." would just have a boolean type here. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm frustrated to see that throw expressions cannot be used neatly in return statements, it would be neat if we could write:
return SomeOperation(args) == 0 ? throw new InvalidOperationException("Something bad happened");
If the result did not equal zero then the return simply returns the result of
SomeOperation(args)
.Of course use of
?
requires one to also specify:
but what would that look like in the above code? This is why I ommitted it but if we insisted on retaining:
here then we'd need a new keywordresult
or something (meaning the result that was going to be returned) but adding a new keyword seems overkill hence I suggest making the:
optional where we are using a throw expression.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions