Tuesday, July 26, 2016

Swift Type Aliases

Ash Furrow:

After deleting the typealias and replacing it with a class of the same name, we didn’t have to worry about changing function definitions and property types all throughout our codebase. We made the changes locally, in one file, and most all the rest of our code still compiled. Pretty cool! Here’s the relevant portion of the pull request that made that change.


Any time you use the same tuple type more than once, consider making a typealias. In this case, the code became a lot shorter and easier to skim and understand.


Objective-C developers, burdened with arcane syntax for blocks, use C’s typedef to isolate that syntax strangeness in one place. And even though Swift’s closure syntax is awesome, we can still benefit from Objective-C’s example – we can use type aliases for closure signatures.


I recommend using a descriptive typealias that is private to your file, and then extending that typealias so you can keep things neat and tidy. […] We’re still extending the view controller, but specifically we’re extending the typealias so that the extension has a helpful name. This is another way that typealias can help add semantic meaning to your code.

Too bad Swift extensions can’t be named directly, like Objective-C categories.

1 Comment RSS · Twitter

[…] Previously: Swift Type Aliases. […]

Leave a Comment