-
Notifications
You must be signed in to change notification settings - Fork 4.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
Proposal: Make "throw expression" an expression form #5143
Comments
This is a wonderful idea. It is simple, it paves the way for future constructs. It will improve many existing language features, such as lambda expressions, and by extension that will improve the elegance of certain patterns, such as dispatch dictionaries. |
I like this solution, but It can be implemented today with this nice trick: public static T Throw<T>(this Exception ex)
{
throw ex;
} And used like: var foo = term.HasValue ? DateTime.Now : new ArgumentException("term").Throw<DateTime>(); Just my 2 cents... |
@olmobrutall That does not have the correct definite-assignment behavior. |
@gafter Can you elaborate on that? I need an example to understand the following sentence
Still, just for the sake of discoverability, I like your proposal very much. |
Yes, what that means is that in an expression of the form Exception x = new Exception();
var q = condition ? 2 : throw x; // is x definitely assigned here? We need the quoted rule to answer that question. |
Mmmm... Then what's the problem with the |
Oh, I know. You mean for the trivial case. This won't compile because no value is returned: public int Foo(){
string s = new Exception().Throw<string>();
} In general this is not a problem because you use it in conditional expressions but this is another point to implement your proposal. |
@alrz I don't get it. What would be the type of returned object? |
@alrz So, you want to change the behavior of |
@stepanbenes Sorry I made a mistake, |
@alrz So, the return expression indicates that the method does not return? I am confused. |
@alrz The problem is that some value has to be assigned to variable |
With the proposed construct: int n;
M(condition ? (n = 12) : throw X,
n); // ok, n is definitely assigned here but with int n;
M(condition ? (n = 12) : Throw<int>(X),
n); // oops, n is not definitely assigned here This is the second bullet about definite assignment. Stated in isolation, the rule is
|
Implemented the match expression. dotnet#5154 Implemented the throw expression. dotnet#5143
All: A draft spec, roughly up-to-date, for pattern-matching is now in review #6570 and includes this feature. |
shouldn't do {
let result = F() switch(
case Some(value): value,
case None(): continue
);
} while( ... ); I know this is possible with Then this one would be a viable option (combining #1938 and #6877) from item in list
let Some(value) = item else continue
do WriteLine(value);
// equivalent to
foreach(var item in list) {
let Some(value) = item else continue;
WriteLine(value);
} |
@alrz The short answer is "no". Linq queries are not, in general, equivalent to loops unless they happen to use MS's |
Speaking of LINQ, maybe the new throw expression should be emitted as an expression tree as well: https://github.com/dotnet/roslyn/issues/2060 |
@gafter In Rust that would be possible, let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue, // continue expression
};
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
} |
This has been folded into the pattern matching spec. |
The spec has been moved
The spec for the proposed throw expression has been moved to https://github.com/dotnet/roslyn/blob/future/docs/features/patterns.md
Below is a snapshot that may be out of date.
I propose to extend the set of expression forms to include
The type rules are as follows:
The flow-analysis rules are as follows:
A throw expression is allowed in only the following contexts:
?:
?:
??
This proposal is intended to facilitate a move toward expression-oriented programming, adding convenience in a number of scenarios. For example
An expression-bodied method may now throw
A conditional expression may throw on one branch:
I am proposing this to facilitate a proposal (#5154) for an expression form of a pattern-matching switch, where one may want to throw in some branch.
This is related to #59 and #1226.
The text was updated successfully, but these errors were encountered: