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.

Swift Snippets 🚀

It’s an initiative to share snippets that leverages the…

John Sundell

Written by

Find my weekly Swift articles on swiftbysundell.com. I’m no longer publishing on Medium.

Swift Snippets 🚀

It’s an initiative to share snippets that leverages the great power of Swift 🤘

John Sundell

Written by

Find my weekly Swift articles on swiftbysundell.com. I’m no longer publishing on Medium.

Swift Snippets 🚀

It’s an initiative to share snippets that leverages the great power of Swift 🤘

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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