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