Swift snippet #12 — Using standard library names for your types

John Sundell
Mar 28, 2017 · 1 min read

Tuesday, 28th March, 2017

You can find its Gist here!

Sometimes the most appropriate name for a nested type is a name that is already taken by the Swift standard library. At first, it may seem impossible to use these names, since you’ll get errors like this:

Circular enum raw types Error 😓

But the good news is that you can just add Swift. to disambiguate between your type and the standard library type!

Using the above snippet, you can now simply reference Error whenever you want to throw an error within Command 🚀, like this:

struct Command {
private let string: String
init(string: String?) throws {
guard let string = string else {
throw Error.missing
}
guard string.isValidCommand else {
throw Error.invalid(string)
}
self.string = string
}
}

This post is a guest post for the Swift-Snippets publication, which you can find out more about here. I also post weekly blog posts about Swift development which you can find here. You can also find me on Twitter @johnsundell.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store