5 ms·
Exactly. It isn't just that it's shorter; it fixes a right-left-right-left alternating reading order, turning it into a left-to-right reading order.
by JoshTriplett 10y ago
Exactly. It isn't just that it's shorter; it fixes a right-left-right-left alternating reading order, turning it into a left-to-right reading order.
- Scaevolus 10y agoI think it also lets you turn this: try!(baz(try!(bar(try!(foo()))) into baz(bar(foo()?)?)? Which isn't as bad as the alternating order for chained evaluation, but cleans up a lot of the noise.
- JoshTriplett 10y agoTrue, though in some cases you may want to write that one as: foo().and_then(bar).and_then(baz) That becomes a bit more verbose, but it reads left-to-right, and works better if you want to use a closure as one of the functions. Which form looks more readable in a given circumstance may depend on the nature of foo, bar, and baz.
- masklinn 10y agoIt has somewhat different semantics though, for instance with `?` all three functions can use different error types.
- richardwhiuk 10y agoThey have to be convertable though, because the function must have a single result code.
- lisivka 10y agoWhat if I want to do something like in following snipet: baz(foo)?("Cannot do baz, foo={}", foo).bar()?"Cannot do bar"; Is there a syntax sugar for such thing? If so, I will be converted to Rust.
- steveklabnik 10y agoIt would work with just the ?s if you had the right implementations of the error conversion functions. I have an example below.
- lisivka 10y agoCompiler cannot determine intent, so it will not be able to produce correct, user readable error message, in user language.
- steveklabnik 10y agoYes, that's why you end up writing an error type and the conversions between various errors.
- lisivka 10y agoImagine, you are taking exam. If you found incorrect question in exam, then it is a bug. If you answered all questions but wrongly, then it is a failure. If you answered correctly to all answers but examiner said that you failed, then it is an error. In case of bug, code must panic. In case of failure, code must return a result, which clearly says that this is the failure. Try next time. In case of error, code must print informative description of error, bunch of data, and maybe some hints to operator, so he will be able to fix it. We are talking about errors, right? Why I need conversions to display bunch of strings with descriptions and variables to an operator? Just let me collect strings and print them to a log.
- steveklabnik 10y ago> Just let me collect strings and print them to a log. If that's something you want to do, that's quite easy. Vec<String> is your error type. Done. But if you want to build something a bit richer, for example, to differentiate between the case in which you answered a question wrong, or when the examiner fails you, you could do that as well. It all depends. But errors as values is what allows you to have that flexibility; it's easily extensible.
- bluetech 10y agoHas anyone suggested some sort of postfix macro syntax? I don't have one in mind, but perhaps there is some nice looking, viable one. That might have been a more general solution than a new operator.
- JoshTriplett 10y ago> Has anyone suggested some sort of postfix macro syntax? Yes, numerous people. A few different discussions have gone by; it has many potential applications, but it introduces several aspects of syntactic weirdness and parsing weirdness, so it'd require a lot of care to introduce if at all.