Swift Diffs

This is one of those blog posts which is really just note taking. I’m basically writing this for myself. It may be useful to people who aren’t me.

This exercise doesn’t include stdlib diffs.

Error Handling

Defer, guard, trythrow, do{}, catch statements added for error handling. And throws attribute can be added to function/method declarations.

do {
    defer {print("Done now")}
    guard let x = optionalValue else {
       print("Fail")
       throw NSError(domain: "", code: 0, userInfo: nil)
    }
    print(x) // if not optional
}

Statements

Do-while is now repeat-while, enabling code reading to distinguish difference at the start of the declaration.

repeat {print("Hello")} while false

For in adds where clause (for-in-where)

for i in 0...20 where i % 2 == 1 {
    print("odd: \(i)")
}

While statement and optional binding tweaked but I don’t think people will really notice

New if-case added. The first examples bind the value within the Optional enumeration. The last example uses the new ? operator.

if case .Some(let x) = optionalValue {print("Unwrapped: \(x)")}
if case let .Some(x) = optionalValue {print("Unwrapped: \(x)")}

if case let x? = optionalValue {print("Unwrapped: \(x)")}

For-case

let z : [Int?] = [1, 3, nil, 5, nil]
for case let x? in z {print(x)} // prints 1, 3, 5
for case let .Some(x) in z {print(x)} // prints 1, 3, 5

If statements now support statement labels, added to existing loop and switch constructs.

With regard to parameters, default label behavior has changed and functions now match methods. Hashtag # (internal-external label match) is gone.

Availability

Availability via @available, #available added. Should improve in later betas. Platforms expand from iOS and OSX to include watchOS, iOSApplicationExtension and OSXApplicationExtension.

 

2 Comments

  • Did likely the same while taking notes on the What’s new in Swift – Talk (Video).
    https://www.dropbox.com/s/sx9sn4qigpyjdw5/Talk%20106%3A%20What%E2%80%99s%20new%20in%20Swift.pdf?dl=0

    I tried the new markdown-formatting in the playground and did not get the old “:param:”-statement to work… Have you tried this? Do you know if they’ve changed it?

  • I find the grammar change from “do.. while” to “repeat.. while” interesting. Is Swift syntax *intentionally* mirroring the late-great-Apple-creation of HyperTalk? I’ve always thought that was the biggest ball Apple ever dropped.

    I also find interesting the “do {try .. } catch” idiom mirrors the “try .. catch .. catch_all” of the Newton SDK. (IMO that’s the other second ball Apple dropped.. at least for ten years until the iPhone, which was reasonably-sized, and didn’t the “touch-equivalent” (?) of an “Egg Freckles” pre-Rosetta recognizer.

    IMO Apple should have *started* with the Newton 2100, with a reasonable processor and amount of memory, two expansion ports (CompactFlash//MMC if they were around yet), maybe even USB (though Apple’s always hated open-standards especially under Jobs), and finally *with* the flip-lid keyboard they were probably planning (cf the hidden interconnect port – much like the Apple Watch now)

    * Why would you want to “try” without a “do .. catch” block wrapping it ??

    ** #2 Does any enterprising Cocoa hacker know if the gesture/multi-touch recognition system has anything to do with how the TStroke/TStrokeBundle/TRecognizer/TClickRecognizer/TGestureRecognizer/TRecognitionManager system used by NewtonOS worked? (I suppose some might be in InkWell API, though I don’t know if there *is* an InkWell API)