Extend emun case

Hi


I'm using enum and wanted to write an extension to expose additional case values. Just confirming that this is not possible in Swift 5.1. I was thinking something like:

import CryptoKit

extension CryptoKitError {
    enum `Self` : Error {
    /// The data used to deserialized a key with `SecKeyCreateWithData` was incorrect.
    case incorrectKeyData
    
    /// The generation of the failed.
    case keyFailure
    }
}


The closest usage I example is:

throw CryptoKitError.Self.keyFailure


What I'd really want is:

throw CryptoKitError.keyFailure


Thanks

Answered by Claude31 in 388886022

I find it logical, as enum extension is defined as properties of Self.


You cannot extend enum directly with:

extension CryptoKitError { 
    case incorrectKeyData 
    case keyFailure 
}
Accepted Answer

I find it logical, as enum extension is defined as properties of Self.


You cannot extend enum directly with:

extension CryptoKitError { 
    case incorrectKeyData 
    case keyFailure 
}

Hi

I thought that might be the case that I couldn't extend an enum directly.

That could be worth a question on Swift.org.


Don't forget to close this thread anyway.

yep, no worries

I can't imagine this ever being possible because of all the existing code designed to address every possible outcome in a switch/case. Suddenly none of it would be exhaustive. It would break all of that.

I can't imagine this ever being possible because of all the existing code designed to address every possible outcome in a switch/case. Suddenly none of it would be exhaustive. It would break all of that.

Swift has the facilities to deal with that in some cases. See SE-0192 Handling Future Enum Cases. However, this only allows the original enum declaration to add new cases. craigaps is trying to add new cases in an extension, something that’s not currently possible. Adding such a feature is not beyond the realm of possibility, although such discussions are best held on Swift Evolution.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Extend emun case
 
 
Q