News for hackers. Stuff that matters.
Re:Swift 1.2 and Xcode 6.3 beta by archagon on Monday February 09, 2015 @03:41PM (#9023554)
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.
Re:Swift 1.2 and Xcode 6.3 beta by masklinn on Monday February 09, 2015 @04:01PM (#9023693)
I'd like to see them expand it the way Rust did: Rust lifted `if let` from Swift (I believe) but made it a more general-purpose refutable pattern matcher, it's just one step below match and works with any enum type not just Option. So you can write:

    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.
Re:Swift 1.2 and Xcode 6.3 beta by oleganza on Monday February 09, 2015 @04:34PM (#9023897)
Swift borrowed "if let" from Rust, not the other way around.
Re:Swift 1.2 and Xcode 6.3 beta by CUViper on Monday February 09, 2015 @04:47PM (#9023968)
"The if let construct is based on the precedent set by Swift,"

https://github.com/rust-lang/rfcs/blob/master/text/0160-if-l...

Re:Swift 1.2 and Xcode 6.3 beta by eridius on Monday February 09, 2015 @05:36PM (#9024248)
That's putting it a bit mildly. Speaking as the author of that RFC, Rust's "if let" would never have existed if it were not for Swift. When I saw Swift's "if let" I immediately knew I wanted it in Rust. I also generalized it to arbitrary patterns at that time, since Rust doesn't fetishize Option the way Swift fetishizes Optional.

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.

Re:Swift 1.2 and Xcode 6.3 beta by oleganza on Monday February 09, 2015 @04:57PM (#9024038)
Wow. Such a rapid feedback loop here. Thanks for sharing.
Re:Swift 1.2 and Xcode 6.3 beta by WildUtah on Monday February 09, 2015 @05:34PM (#9024233)
if-let is great. I don't know where it started (apparently upthread they say Rust and swift have been swapping the idea) but Clojure has had it since 2008.[0]

    (if-let [name name-whose-value-is-possibly-null]
      (something-to-do name))
[0] https://clojuredocs.org/clojure.core/if-let
Re:Swift 1.2 and Xcode 6.3 beta by loqi on Monday February 09, 2015 @08:07PM (#9024809)
C++98 (maybe earlier) has a similar feature:

    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);
Re:Swift 1.2 and Xcode 6.3 beta by melling on Monday February 09, 2015 @05:52PM (#9024367)
I imagine everything in Swift has been borrowed from another language. OCaml, Go, etc. I'd be more interested in other great language features that are missing from Swift that are in other "classic" languages like OCaml, for instance.
Re:Swift 1.2 and Xcode 6.3 beta by wyager on Monday February 09, 2015 @06:44PM (#9024557)
Why is "if let" preferable to generalized pattern matching?
Re:Swift 1.2 and Xcode 6.3 beta by masklinn on Tuesday February 10, 2015 @03:59AM (#9026053)
It's not "preferable" in the sense that you should have if let and not pattern matching. In Rust (and probably in Swift) it builds upon (and maybe desugars to, not sure) pattern matching. `if let` tends to be shorter and more readable than a full-blown match with a single match branch or just match/fail. Chainable `if let` is also significantly more convenient when trying multiple alternatives (on different source variables) one after the other.

See the Rust RFC on the subject for motivations and comparisons: https://github.com/rust-lang/rfcs/blob/master/text/0160-if-l...

Re:Swift 1.2 and Xcode 6.3 beta by archagon on Monday February 09, 2015 @06:57PM (#9024615)
I'm not a language guy so I don't know anything about generalized pattern matching. Maybe it is better. But for my own use, "if let" ensures that I always take care of any possible nulls in my code, while also keeping the conditional code in the same scope as the verified variable. There's no easy way to leave pointers dangling or uninitialized. The language design forces me to organize and think about my code in a much better way.
Re:Swift 1.2 and Xcode 6.3 beta by wyager on Monday February 09, 2015 @09:46PM (#9025111)
With generalized pattern matching, your code could (among other things) take the form of

    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.
Re:Swift 1.2 and Xcode 6.3 beta by archagon on Monday February 09, 2015 @10:09PM (#9025176)
I'm not sure if this is the case in your example, but at least in Swift, the "if let" block or chaining for optionals is mandatory. This should mean that it's impossible to have a dangling/null pointer unless you use the force dereference operator. (And that's something that should almost never appear in your code!) You don't get the choice of being lazy or forgetting about checking your pointers: it's a conscious decision either way.

Personally, I appreciate that the language naturally makes me organize my code better and more safely.

Re:Swift 1.2 and Xcode 6.3 beta by masklinn on Tuesday February 10, 2015 @03:55AM (#9026035)
It is, and you can pattern-match Optional in Swift, something along the lines of

    switch optional {
    case .Some(let numeral):
        println("Caught a \(numeral)")
    default:
        println("Nothing to catch")
    }