Archive for April 30, 2018

Monday, April 30, 2018

Reimplementation of Implicitly Unwrapped Optionals


The new mental model for IOUs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.


There are three places where using ! as part of a type is permitted:

  1. Property declarations
  2. Parameters in function declarations
  3. Return values in function declarations


Because implicitly unwrapped optionals are no longer a type distinct from optionals, they can’t be inferred as a type or as any part of a type.


Note that the result of AnyObject lookup is treated as an optional that is implicitly unwrapped. If you lookup a property that itself is also declared as implicitly unwrapped, the expression now has two levels of implicit unwrapping[…] if let and guard let only unwrap a single level of optionality.

NSColor, CGColor, and Layers

Jeff Nadeau (tweet):

NSColor is dynamic, so its exact color value depends on the view’s context. CGColor isn’t dynamic, so resolving a CGColor produces a snapshot from the current context at the moment you ask for it. As your view’s context changes, you’ll be left with a stale value that might look very wrong compared to the surrounding UI.


Besides the obvious NSAppearance changes which can occur (vibrancy, Increased Contrast mode, etc.), there are more subtle ones. For example, in the Touch Bar there are specific elements of the UI that get white balanced automagically as ambient light readings change. […] The amount of context-sensitivity is only going to increase over time.


The layer tree is a side effect of the view hierarchy, not its model. This probably annoys UIKit people a lot, but this approach is extremely intentional.