Re: [swift-users] Refining generics in classes

2017-11-16 Thread Itai Ferber via swift-users
Hi Jon, To clarify for those wondering, this is happening because the `doAThing()` method dispatches statically to `doSomething()` based on `NetworkRequests`’s `T`; since `doAThing()` isn’t overridden in `CompressedNetworkRequest`, the method is inherited directly, including that static

Re: [swift-users] Parsing Decimal values from JSON

2017-10-31 Thread Itai Ferber via swift-users
coming up, and the performance overhead of JSONSerialization with JSONDecoder on top of it will continue to leave Swift without a very performant JSON solution. That said, I appreciate the support given Codable on this list. Jon On Oct 31, 2017, at 1:07 PM, Itai Ferber via swift

Re: [swift-users] Parsing Decimal values from JSON

2017-10-31 Thread Itai Ferber via swift-users
Hi Evtim, Just want to give some context for this. This is due to the fact that `JSONEncoder` and `JSONDecoder` are currently based on `JSONSerialization`: when you go to decode some JSON data, the data is deserialized using `JSONSerialization`, and then decoded into your types by

Re: [swift-users] Array not Encodable - error at runtime, not compile time?

2017-10-26 Thread Itai Ferber via swift-users
Hi Robert, When the conditional conformance feature arrives in Swift, it will allow us to express `extension Array : Encodable where Element : Encodable` and `extension Array : Decodable where Element : Decodable`. At the moment, this isn’t possible, so `Array` is unconditionally `Codable`

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

2017-10-19 Thread Itai Ferber via swift-users
Right now, collection types in the standard library (`Array`, `Dictionary`, and `Set`) employ a workaround for the lack of conditional conformance, which would allow us to expose things like `extension Array : Codable where Element : Codable` (i.e. `Array` is `Codable` if and only if `T` is

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

2017-10-19 Thread Itai Ferber via swift-users
Mm, the point I’m trying to get at here is that JSON is inherently untyped while Swift is strongly typed, and the two don’t line up. It doesn’t really make sense to ask to decode an existential because there’s not enough type information to influence what you get back. On 19 Oct 2017, at

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

2017-10-19 Thread Itai Ferber via swift-users
Even then, that wouldn’t necessarily help in the general case. If you decode `{"key" : 1}` as `[String : Codable]`, what concrete type would `1` have? `Int`? `Double`? `Int8`? (Arguments can be made for any one of these, but the key here is that it is inherently ambiguous and there isn’t

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

2017-10-19 Thread Itai Ferber via swift-users
Then no, this wouldn’t be possible unless you could somehow express something like: ```swift // Cribbing some C++-style syntax here enum OneOf : Codable where T… : Codable { cases t…(T…) } ``` where someone would be able to express to you that they want to store a `OneOf

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

2017-10-19 Thread Itai Ferber via swift-users
David, Is there an issue with extending the enum as necessary with new cases to support what you need? ```swift enum MyType : Codable { case int(Int) case string(String) case newThingy(NewThingy) case list([MyType]) case dictionary([String : MyType]) // … } ```

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

2017-10-19 Thread Itai Ferber via swift-users
Hi Geordie, Yep, that’s the difference here — you can’t decode something with an existential type; it has to be concrete (like you’re doing). The reason for this is that it’s simply not possible to determine what type to decode just from the structure of the payload. Consider the following:

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

2017-10-19 Thread Itai Ferber via swift-users
Why are you stuck? I think the following matches your needs, no? ```swift import Foundation enum MyType : Codable, Equatable { case int(Int) case string(String) case list([MyType]) case dictionary([String : MyType]) public init(from decoder: Decoder) throws { // Can

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

2017-10-19 Thread Itai Ferber via swift-users
Hi David and Geordie, That approach won’t work — encoders and decoders only work directly with concrete `Codable` types (e.g. `String`, `Int`, `MyFoo` [where `MyFoo` is `Codable], etc.). This is by design: since there is no type information stored in the JSON payload, there isn’t necessarily

Re: [swift-users] No Generic Decoders?

2017-09-05 Thread Itai Ferber via swift-users
Hi Jon, This was a conscious choice in the API design, for three main reasons: 1. There is a big difference between the API surface exposed at the top-level for encoding and decoding and the middle levels. A method like `decode(_ type: T, from data: Data)` is really only appropriate at the

Re: [swift-users] Swift 4 emulating Decoder behaviour

2017-07-14 Thread Itai Ferber via swift-users
Hi Joanna, Your example doesn’t work for a few reasons: 1. You’re getting a compiler error because of the difference between the representation of metatypes (`Foo.Type` for some type `Foo`) of concrete types (e.g. `String`, `Int`), and protocols (e.g. `DefaultValueProvider`). Some of the

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-07-03 Thread Itai Ferber via swift-users
Hi Kevin, You’re right — this is one of the limitations of the box design here. One thing we can do is expose the underlying boxed value as a `KeyedDecodingContainerProtocol` existential value using an accessor on the box so you can down-cast. However, it shouldn’t be necessary to add any

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-29 Thread Itai Ferber via swift-users
Hi Kevin, > On Jun 29, 2017, at 12:30 PM, Kevin Wooten via swift-users > wrote: > >> Hi Jon, >> >> I just joined this mailing list and have tried to catch up on the >> history of this thread, so please excuse me if I’ve missed something. >> >> I’m sorry the Codable

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-23 Thread Itai Ferber via swift-users
Hi Jon, I just joined this mailing list and have tried to catch up on the history of this thread, so please excuse me if I’ve missed something. I’m sorry the Codable API at the moment does not answer your needs — you’re clearly not the only one who’s run into this, so let’s see how we can