With the introduction of Swift iOS and Mac developers keen in writing good semantic code saw a dream come true. The simplicity and power of the Swift’s syntax really opens the door for a cleaner and more readable code. I recently read a post on the meaning of “readable” code, so I wanna specify that here readable means self-explanatory and that “reads like english” as much as possible.

One of the techniques I like to use to make code more readable is redefining basic types with domain specific names. I first read about this here, but I assume is a well known technique.

For example, given an Athlete class, the method medalsWonCount won’t return an Integer, but a Medals type.

Another example: given a Contact class, the emailAddress method won’t return a String, but an EmailAddress type.

Wrapping primitive types and strings in domain specific types certainly adds an overhead at the time of writing, but makes for clearer code. And remember that code is read more time than it’s written.

So, how do you redefine a type in Swift?

typealias Email = String

let email: Email = "giovanni.lodi42@gmail.com"