Wednesday, July 19, 2017

Strongly Typed Identifiers in Swift

Tom Lokhorst:

We implement the Hashable and Equatable protocols so we can use these identifiers in Sets and as Dictionary keys.

We use RawRepresentable so we get the Equatable implementation for free, Hashable is implemented in a protocol extension[…]


Haskell has a language feature that implements this pattern of wrapping an existing type to create a new type. It is called: newtype.

That would also be nice to have in Swift. It looks similar to typealias, but creates a new type, instead of just an alias.

Otherwise there is a lot of boilerplate. He presents several ways of reducing it.

Comments RSS · Twitter

Leave a Comment