[swift-users] dealing with heterogenous lists/dictionary with Codable

Itai Ferber iferber at apple.com
Thu Oct 19 15:11:05 CDT 2017


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171019/e4f1d081/attachment.html>


More information about the swift-users mailing list