5 ms·
fun read(path: &Path): io.Result[Vec[UInt8] { var file = File.open(path).orReturnErr() var bytes = Vec.new() file.readToEnd(&mut bytes).orRetu
by oxnrtr 5y ago
fun read(path: &Path): io.Result[Vec[UInt8] {
var file = File.open(path).orReturnErr()
var bytes = Vec.new()
file.readToEnd(&mut bytes).orReturnErr()
Ok(bytes)
}
would be good enough for me, tbh.
- yakubin 5y ago> File.open(path).orReturnErr() Method syntax is reserved for functions. There are no method syntax macros. Since a return in a function terminates execution of this function, and not its caller, your example would not work.
- zozbot234 5y agoRight, so this should really be .orReturnErr , by analogy with .await . We just overload the dot symbol to mean both "method call" and "weird monadic stuff is happening here".
- simon_o 5y agoExactly. ! for macros was a mistake anyway, I guess that's why they stopped using it with await.
- yakubin 5y agoIt wasn't. Without "!" it isn't obvious that you cannot take a: let a: Result<u8, u8> = /* whatever *; let b = match a { Ok(v) => Ok(foo(v)), e => e, }; and transform it into: let a: Result<u8, u8> = /* whatever *; let b = a.map(foo); if "foo" is a macro. When you substitute "foo!" for all the "foo" above, it is obvious.
- steveklabnik 5y ago> I guess that's why they stopped using it with await await cannot be a macro, it has to be built into the language, and so it doesn't have the ! because it's not one.
- simon_o 5y agoYou realize the syntax has changed more than that?
- yakubin 5y agoYes, but "->" vs ":", or kinds of brackets are superficial, while method syntax has a specific meaning.
- scottlamb 5y agoThese differences are mostly superficial. We all need to decide if we're going to avoid a language because its punctuation choices aren't "good enough" for us or if we're going to choose the best tools for building useful systems. "orReturnErr()" seems quite against the Rust philosophy though — refusing the method call syntax for early return means folks can easily miss something important. Rust uses ? for error handling, ! for macros, and keywords (with no parenthesis) for other control flow. I prefer it that way.