[swift-evolution] [Proposal] Enums with stored properties
Sean Heber
sean at fifthace.com
Wed Oct 12 14:27:30 CDT 2016
> With the improved syntax, this could look something like this instead:
>
> enum FileError: Error, LocalizedError {
> var url: URL { get }
>
> case notFound(url: URL) {
> errorDescription = "Could not access the file \(url.lastPathComponent) because it could not be found."
> failureReason = "The file \(url.lastPathComponent) could not be found."
> recoverySuggestion = "Please locate the correct file and try again."
> helpAnchor = "notFound"
> url = url
> }
>
> case accessDenied(url: URL) {
> errorDescription = "Could not access the file \(url.lastPathComponent) because access was denied."
> failureReason = "We do not have permission to view the file \(url.lastPathComponent)"
> recoverySuggestion = "You can change the file's permissions using the Finder's Get Info window."
> helpAnchor = "accessDenied"
> url = url
> }
>
> case incorrectFormat(url: URL) {
> errorDescription = "Could not access the file \(url.lastPathComponent) because it was not in the expected format."
> failureReason = "The file \(url.lastPathComponent) was not in the expected format."
> recoverySuggestion = "The file may have become corrupt."
> helpAnchor = "incorrectFormat"
> url = url
> }
>
> case ioError(url: URL) {
> errorDescription = "Could not access the file \(url.lastPathComponent) because an I/O error occurred."
> failureReason = "An I/O error occurred while accessing the file \(url.lastPathComponent)."
> recoverySuggestion = "Dear Lord, the hard drive may be failing."
> helpAnchor = "ioError"
> url = url
> }
>
> // ... etc ...
> }
>
> I don’t think it can be denied that the second is orders of magnitude easier to read and comprehend.
>
> Charles
I’m 100% in favor of something approaching this syntax where the case-specific values are all grouped by the case and not the other way around.
l8r
Sean
More information about the swift-evolution
mailing list