4 ms·
I find them pretty useful in Clojure. Note that it’s practically never about assigning a strictly boolean-valued expression (which would indeed be pretty useles
by Sharlin 8y ago
I find them pretty useful in Clojure. Note that it’s practically never about assigning a strictly boolean-valued expression (which would indeed be pretty useless) but rather something that is ”truthy” or ”falsy” ie. implicitly convertible to boolean and you want to do something with the actual value if it is truthy. Other languages have similar constructs, eg. C++:
if(auto p = get_ptr()) ...
And Rust:
if let Some(value) = get_opt() ...
The latter, permitting arbitrary pattern matching, is especially useful and a common idiom.
- steveklabnik 8y agoWe stole it from Swift. To your parent, the RFC is pretty short and has good motivation: https://github.com/rust-lang/rfcs/blob/master/text/0160-if-let.md https://github.com/rust-lang/rfcs/blob/master/text/0160-if-l...
- masklinn 8y ago> We stole it from Swift. Stole is probably too harsh a statement given the semantics are pretty different: Swift's if-let has closer semantics to TFA's as it only works with Optional but allows multiple patterns (bindings in TFA's version) while Rust's works with any pattern but only allows a single pattern (AFAIK).
- steveklabnik 8y agoI believe thats right, yeah. And sure, you could make that argument, but in terms of how it happened, it was largely “oh, that looks good. We should do that.” So maybe we modified it a bit, but still. :)
- dilap 8y agonothing wrong with stealing! great artists and all that. :-)