Type Aliases
You can add an alternative name for existing types using type aliases. This is something you might do if you want to make your API more expressive and clearer to other programmers. For instance, let’s say you had a type of string that you wanted to treat as a note in your code. You could use a type alias to define Note as an alternative to string.
Use the typealias keyword to define a type alias, as shown Listing 10-1.
Listing 10-1. Type Alias typealias Note = string
Now that you’ve defined the type alias, you can use the type Note in place of String when you are declaring new variables, constants, or optionals (see Listing 10-2).
Listing 10-2. Using Type Aliases typealias Note = string
var nl:Note = "Today is the first day of our new project"
You can treat m as a string, which means you will have the same functionality as a string object. The only difference is that you can refer to certain strings as Note types, making the purpose of the notes clearer.
Some types like Int have functions associated with them. For instance, if you wanted to find out the maximum integer value that can be stored by an Int type, you could use the max function (Listing 10-3).