Again, factually wrong: Try in std::ops - Rust
Okay, there is a nightly trait. Thank you for pointing that out.
For the same reason why we have ? operator instead of try!, which too “saves bunch of characters and two braces”.
Now, this is completely different.
- It's not just bunch of letters, it's a large 5-lines
match
with lots of code. - You are likely to error propagate on each line, that makes code with
Try
trait 5x times smaller and readable.
There is a point where saving bunch of characters stops to be valuable enough to implement it. Allow me to write ?
instead of complex match. Okay, I buy it. Allow me to write throw err
instead of return Err(err)
? Thank you, not today.
Look at these examples:
if condition {
return Err(Foo)
} else {
return Ok(Bar)
}
and it's replaced by:
if condition {
throw Foo
} else {
return Ok(Bar)
}
Is it better in any way? I don't think so. I see function definition that returns Result<Foo,Bar>
. I see honest return Ok(Foo)
and return Err(Bar)
, they match function result type Result<Foo,Bar>
which I know is Either[Ok(Foo), Err(Bar)]
. As a result it's clear for me what happens here, it's consistent and nice. Why whould I like to have this throw
? Because I want to save four chars and two braces?
More consideration: how this function is called? I believe it's
match foobar() {
Ok(foo) => { ... }
Err(bar) => { ... }
}
which again is clear because we either construct Ok(foo)
or Err(bar)
and deconstruct them in the same manner. We don't write
match foobar() {
Ok(foo) => { ... }
catch bar => { ... }
}
While it's reasonable to expect that you can catch
something you have been thrown
. But you can't. At least, at this point. But nothing to stops us from implementing it a bit later, when throw
gets implemented and they create an RFC with "well, people is confused with throw Err
that cannot be catched, so let's add this one too
It has completely not enough value to be a good language change.
I need to take a break. I just appeared in the thread because @td_ says that there is only bunch of peoples that disagree with proposal. It's not true, we are just silent.