If let (and optionals in general) is my favorite Swift feature and has immediately made my code 10x more stable from the get-go. Glad to see it get expanded like this.
enum Foo {
Bar,
Baz(i32),
Qux(f64)
}
if let Bar = item {
// do something
} else if let Baz(val) = other {
// do something with val
}
which is oft shorter than a full-blown `match`, and more convenient when alternating on different "source" items.https://github.com/rust-lang/rfcs/blob/master/text/0160-if-l...
I am glad to see Swift 1.2 expanding "if let" to multiple clauses. But I do wish it supported arbitrary values instead of just Optional.
(if-let [name name-whose-value-is-possibly-null]
(something-to-do name))
[0] https://clojuredocs.org/clojure.core/if-let if (mytype* p = find_or_null(blah))
foo(*p);
Still have to dereference p, but at least it's only in scope when doing so is safe. Also goes well with auto and std::optional: if (auto opt = find_or_none(blah))
foo(*opt);See the Rust RFC on the subject for motivations and comparisons: https://github.com/rust-lang/rfcs/blob/master/text/0160-if-l...
case result of
Just x -> <do something with x>
Nothing -> <do something without x>
This works with optionals as well as any other algebraic data type.Personally, I appreciate that the language naturally makes me organize my code better and more safely.
switch optional {
case .Some(let numeral):
println("Caught a \(numeral)")
default:
println("Nothing to catch")
}