enums – Passing a case of kind “perform” as a parameter in Swift

enums – Passing a case of kind “perform” as a parameter in Swift


I have been utilizing Codable enums to encode my courses to JSON, and I’ve hit a roadblock:

enum MyEnum: Codable {
    case case1(knowledge: Knowledge)
    case case2(str: String)
    case case3(url: URL)    
}

// ...

myFunc(myEnumType: MyEnum.case1)

func myFunc(
        myEnumType: MyEnum // ?
    ) -> MyEnum {
                
        swap myEnumType {
        
        case .case1:
            // insert processing...
            return myEnumType(knowledge: ...)
        case .case2:
            // ...
            return myEnumType(str: ...)
        case .case3:
            // ...
            return myEnumType(url: ...)
            
        }
        
        
    }

As a result of MyEnum.case1 is of kind perform, I could not use MyEnum, so I needed to resort to this very ugly resolution[1] [2] [3]:

enum MyEnum: Codable {
    case case1(knowledge: Knowledge)
    case case2(str: String)
    case case3(url: URL)    
}

// :(
enum MyEnumType {
    case case1
    case case2
    case case3
}

// ...

func myFunc(
        myEnumType: MyEnumType // :(
    ) -> MyEnum {

How can I cross my case to the perform as a parameter?

That is what I do not need:

  1. Very generic kind[4] [5]:
typealias MyEnumType<Arg1> = (Arg1) -> MyEnum

As this might simply be any perform returning MyEnum. This does not assure it is a case of MyEnum.

  1. Initializing the case:
myFunc(myEnumType: MyEnum.case1(knowledge: dummyData))

I do not need to initialize the case with dummy knowledge. myFunc is the one which’s presupposed to initialize the case with precise knowledge, then return the initialized case.

  1. Passing the enum:
myFunc(myEnumType: MyEnum)

That is irrelevant: I need to know what case I am taking a look at within the perform with the intention to init every enum uniquely.

One thing like:

func myFunc<T>(myEnumType: MyEnum.T) -> MyEnum {

Which ensures the uninitialized case I’ve handed to the perform is certainly a case of the MyEnum enum.

I am open to altering the perform name or the perform, so long as I am not initializing the case or having to redeclare each case.


  1. Reply 2: Swift enum property with out initializing the enum case with an related worth?
  2. Permit Swift perform parameter to be of a number of varieties
  3. Swift-Generics: “Can’t specialize non-generic kind”
  4. take a perform of any variety of parameters of any kind as a parameter in Swift
  5. Reply 5: Swift enum property with out initializing the enum case with an related worth?

Related analysis I could not use:

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.