Then no, this wouldn’t be possible unless you could somehow express something like:

```swift
// Cribbing some C++-style syntax here
enum OneOf<T…> : Codable where T… : Codable {
    cases t…(T…)
}
```

where someone would be able to express to you that they want to store a `OneOf<Int, String, Double, MyFoo>` through your enum, or something like that.

You could do that in a non-extensible way with something like

```swift
enum MyEnum<T> : Codable /* where T : Codable */ /* <- when conditional conformance arrives */ {
    case int(Int)
    case string(String)
    case custom(T)
    case list([MyEnum<T>])
    case dictionary([String : MyEnum<T>])
}
```

but that’s not truly heterogeneous without extending with more generic types.

If you don’t know the type you need to decode, then you won’t be able to do this unless the encoder/decoder supports somehow mapping the type to and from data in the payload.

On 19 Oct 2017, at 12:52, David Baraff wrote:

An even bigger “no can’t do that”: the enum would be in some base/low-level library, and thus can’t know about new types that exist in higher-up libraries.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to