-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a function that takes multiple Decodable classes/structs and returns the first to decode #84
Conversation
The variadic list is so that lists of zero length cannot be passed in, preventing runtime errors that you would otherwise bring in with |
Hi, thanks for addressing and sorry for late response, but wouldn't callers always have to force-downcast the result in all circumstances? In that case, although decodeAsOneOf(json, objectTypes: Car.self, Bus.self, ...) as! Vehicle may be prettier (and slightly different?) than (try? Car.decode(json) as Vehicle) ?? Bus.decode(json) as Vehicle ... , I don't like that a type could not conform to |
In my code I've been using optional downcasting, usually with flatMapping since in the JSON I've been working with, this kind of decoding is really helpful with arrays of dictionaries where the data is essentially unofficially subtyped. |
Ah, right, didn't think of that. Hopefully future versions of Swift will allow a more constrained version though. Could you add a short test case, mainly for adequate example-usage in a new file? |
In a completely new file or in an existing test file? |
I would say new, like |
Ugh, Travis is on an old version of Xcode. The tests I committed are building fine in 7.3. |
Thank you very much! And sorry about travis, will fix at some point. |
Inspired heavily by the Elm JSON.Decode function
oneOf