155 points · by glhaynes · 11y ago · 87 comments
That's a nice example of "if you're not embarrassed of 1.0, you shipped too late".
The compile time caused by this issue was making the development process really slow. We had to split the whole application into mini sub projects to make it bearable.
https://twitter.com/practicalswift/status/564921024782041088
https://github.com/practicalswift/swift-compiler-crashes/com...
Translation for those unfamiliar with Swift: Swift has a relatively strong type system. If a variable in Swift can be null at any point, it must be explicitly declared so as part of its type (e.g. an integer that can be null has the type "Int?", an integer that cannot be null has the type "Int").
Swift has a way to 'unwrap' these optional values and do things with them if they exist. Prior to this release, unwrapping multiple optional things required a bunch of nested if statements. With this release, unwrapping multiple optional values can be done in one if statement.
This is why it's lame that Swift's optionals are a language feature and not an in-language implementation.
In Haskell, optionals are Monads, which means there is a function
join :: m (m a) -> m a
"Maybe" (Haskell's standard library Optional) is a monad, so you can unwrap/collapse them using Join.You can also daisy-chain them together using >>=, a la
fail1 :: Maybe a
fail2 :: a -> Maybe b
result = fail1 >>= fail2
If either fail1 or fail2 fails, the entire "result" function fails (i.e. returns "Nothing"). 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. (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...
The "wrapping the optionals in a tuple and using switch to pattern match" hack always seemed dirty, and this is a huge step in the right direction for code readability.
iOS 8.2 beta and 8.3 beta
XCode 6.2 beta and 6.3 beta
How is the release cycle going to work? When will 8.2 and 8.3 come out and which is more stable for Swift work.
Optional is a Swift type that contains a value of any type. Obj-C doesn't have anything matching that behavior at all.
What's been done here is Obj-C has gained the ability to annotate any pointer valued type as either nullable or non-nullable. This does not affect the runtime behavior of the code in the slightest. It doesn't even affect the semantics of Obj-C. All it does for Obj-C is let the compiler yell at you if you pass nil to a non-nullable parameter / property.
The real point of these annotations is so Swift can be more intelligent when translating the obj-c API to Swift, choosing to use the correct optional type (or no optional type at all).
I assume that, now that this feature exists, Apple will begin annotating their Obj-C APIs.
"However, nullability annotations—in addition to improving the experience in Swift—provide new warnings in Objective-C"
let x : SomeThing
if condition {
x = foo()
} else { x = bar()
}use(x)
const int a = [=](){
//do stuff here that returns an int:
if (condition) { return foo(); } else { return bar(); }
}();
Does this idiom work in swift? let x: String = {
if 3 == 4 {
return "Hello"
} else {
return "Goodbye"
}
}()
Although in this case, I'd just use ternary. let x = (3 == 4) ? "Hello" : "Goodbye"let x : Something = condition ? foo() : bar()
but yeah, it would be nice to defer initialization of an immutable.
let x: Something = {
if condition {
someSideEffect()
return foo()
} else {
anotherSideEffect()
return bar()
}
}()
But it's kind of ugly.The only reason I can see to not open it up at the moment is that they're not ready for it yet. I reckon it'll come soon enough.
Which is a shame because OSX 10.6.8 was the most stable, zippiest performance OSX ever and still had the nice virtual desktop. Sadly I lament the old virtual desktop. I'd still be on it if I could be.
But in that case, it makes sense to get bug reports from non-developers.