I'm unsure what "miss" means here. If you forget to add a ?, it's a compilation error.
sanitize(getFile(getName())?.contents);
Swift requires prefixing the line with try, which makes it easier to notice: try sanitize(getFile(getName()).contents)
Both are a big improvement over exceptions which have no indication at the call site. sanitize(try getFile(getName()).contents) sanitize((try getFile(getName())).contents)
This sort of illustrates why Rust moved away from its own prefix `try!` macro to its postfix `?` operator: in a language that encourages chaining methods, `foo()?.bar()?.qux()?` looks better than `try!(try!(try!(foo()).bar()).qux())`. func f() throws -> Int { return 0 }
func nothing(_ x: Int) {}
func g() throws {
nothing(try f())
try (f() + f())
try nothing(f() * f())
}
This includes method chains: struct Foo {
init() throws {}
func bar() throws -> Foo { return self }
func qux() throws -> Foo { return self }
}
try Foo().bar().qux()On the other hand error handling can distract from the intention of the code, so in another sense ? makes it easier to read.
The author can choose if they prefer ?, try!, or a plain old match or if let / return on the Result.
> For new features, people insist on LOUD explicit syntax.
> For established features, people want terse notation.
I was pretty against ? when it was proposed. I liked try!. I thought ? was hard to read. I thought try! was concise enough. After writing (and reading) lots of code with ?, I'm all in. Heck, ? still isn't extendable for user-defined types, and now I want that! We made ? work in main, and in tests.
It's now one of my favorite features of Rust. YMMV.