Tuesday, February 2, 2016

Seven Swift Snares

David Ungar (comments):

But with value types, there’s no override keyword to help the compiler find my mistakes. This omission seems out of place in a language otherwise designed to include enough redundancy to help find one’s errors.


All that can be known at compile-time is that pie is a Pizza, and the Pizza protocol extension says Wheat, so the declaration of a cornmeal crust in the CornmealPizza structure had no effect whatsoever when asking pie for something. Although the compiler could have warned about the potential for error from this use of static- instead of dynamic-dispatch, it did not. I believe that here lurks a trap for the unwary, and I would call this a major snare.


Presenting Swift with both a declaration and a definition in this fashion causes the compiler to take notice of the runtime value of the pie variable.


However, without editing the source code in the framework, we cannot fix this problem. Hence, it is impossible to safely extend a protocol declared in another framework (without gambling that it will never need dynamic-dispatch.)


As shown in a previous section, a declaration in a protocol was sufficient to induce dynamic dispatch for a defined of the corresponding attribute in the protocol extension. But a definition in a restricted extension is always statically-dispatched.


Avoid assigning the result of an expression with side-effects to left-hand-side with optional chaining.


In-out parameters do not work when passed into the outer scope of a closure

Joe Groff:

These are all things we hope to remove or provide diagnostics for in 3.0, at least.

Sam Griffith:

Swift for all its newness is going down a path much like C++ did as far as complexity. That is worrisome.


Swift’s init rules are confusing. I’ve never once got anything non-trivial right first time

Marcel Weiher (tweet, first linked in 2014):

Apple’s new Swift language has taken a page from the C++ and Java playbooks and made initialization a special case. Well, lots of special cases actually. The Swift book has 30 pages on initialization, and they aren’t just illustration and explanation, they are dense with rules and special cases.

Joe Groff:

The initializer model does need reconsideration, though. Current model doesn’t fit our ABI resilience goals.

A little up-front complexity for initializers greatly simplifies the state space for everything else.

1 Comment RSS · Twitter

Leave a Comment