dmitri
October 3, 2023, 8:26pm
1
I was wondering why Codable
conformance can't be generated for enums that have overloaded associated values:
enum TestEnum: Codable {
case value(aDouble: Double)
case value(anInt: Int)
}
let t1: TestEnum = .value(aDouble: 1.0)
let t2: TestEnum = .value(anInt: 1)
generates an error (swift-DEVELOPMENT-SNAPSHOT-2023-06-26, arm64 macOS):
**testit.swift:2:6:** **error:** **type 'TestEnum' does not conform to protocol 'Decodable'**
Without conforming to Codable
the enum seems to work:
enum TestEnum {
case value(aDouble: Double)
case value(anInt: Int)
case value(aBool: Bool)
}
let t1: TestEnum = .value(aDouble: 1.0)
let t2: TestEnum = .value(anInt: 1)
let t3: TestEnum = .value(aBool: true)
print(t1,t2,t3)
prints:
value(aDouble: 1.0) value(anInt: 1) value(aBool: true)
tera
October 3, 2023, 8:38pm
2
Not sure about Codable, but JSONEncoder specifically would encode this into something like:
{ "value":{"aDouble":0}}}
and if there are more than one "value" in the resulting (dictionary) "object" that won't fly. Try making different key for each case, e.g:
enum TestEnum: Codable {
case doubleValue(aDouble: Double)
case intValue(anInt: Int)
case boolValue(aBool: Bool)
}
1 Like
dmitri
October 3, 2023, 8:46pm
3
Yeah - this works:
enum TestEnum: Codable {
case doubleValue(Double)
case intValue(Int)
case boolValue(Bool)
static func value(aDouble: Double) -> Self { .doubleValue(aDouble); }
static func value(anInt: Int) -> Self { .intValue(anInt); }
static func value(aBool: Bool) -> Self { .boolValue(aBool); }
}
let t1: TestEnum = .value(aDouble: 1.0)
let t2: TestEnum = .value(anInt: 1)
let t3: TestEnum = .value(aBool: true)
print(t1,t2,t3)
I just wish there was a simpler way, without having to do the duplications.
1 Like
tera
October 3, 2023, 8:50pm
4
I know it's a matter of style which to prefer, but maybe just this?
enum TestEnum: Codable {
case doubleValue(Double)
case intValue(Int)
case boolValue(Bool)
}
let t1: TestEnum = .doubleValue(1.0)
let t2: TestEnum = .intValue(1)
let t3: TestEnum = .boolValue(true)
Otherwise, I guess you'd need to implement Encodable/Decodable conformances manually.