Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unknown default to enums #10

Open
eonist opened this issue Jun 18, 2023 · 0 comments
Open

Add unknown default to enums #10

eonist opened this issue Jun 18, 2023 · 0 comments

Comments

@eonist
Copy link
Member

eonist commented Jun 18, 2023

// Handling future enum cases Swift 5.0

// SE-0192 adds the ability to distinguish between enums that are fixed and enums
// that might change in the future.
// With the @unknown attribute we can now distinguish between two subtly different
// scenarios: “this default case should be run for all other cases because I don’t
// want to handle them individually,” and “I want to handle all cases individually,
// but if anything comes up in the future use this rather than causing an error.”

enum PasswordError: Error {
    case short
    case obvious
    case simple
}

func showOld(error: PasswordError) {
    switch error {
    case .short:
        print("Your password was too short.")
    case .obvious:
        print("Your password was too obvious.")
    default:
        print("Your password was too simple.")
    }
}

func showNew(error: PasswordError) {
    switch error {
    case .short:
        print("Your password was too short.")
    case .obvious:
        print("Your password was too obvious.")
    @unknown default:
        print("Your password wasn't suitable.")
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant