-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
Decoding subclasses #459
Comments
Does it make a difference if you explicitly redeclare |
|
Here's my current thinking on this:
struct Box<T> {
var value: T
}
protocol A {
associatedtype B
static func go() -> Box<B>
}
class Foo: A {
class func go() -> Box<Foo> {
return Box(value: Foo())
}
}
class Bar: Foo {
// error: method does not override any method from its superclass
override class func go() -> Box<Bar> {
return Box(value: Bar())
}
}
print(Foo.go())
print(Bar.go()) In this example, if you replace I'm pretty sure that the ambiguity in the example stems from the child I could be missing something (wasn't able to test against Argo with your specific example, so trying to reproduce with custom types), but I think that explains what's going on here. |
It seams redundant, but does adding |
For now I'm worked around it like this: struct ChildDecoder {
let child: Child
init(title: String, subtitle: String) {
child = Child(title: title, subtitle: subtitle)
}
static func decode(_ json: JSON) -> Decoded<ChildDecoder> {
return curry(Child.init)
<^> json <| "title"
<*> json <| "subtitle"
}
}
private func decodeChildren(key: String, from json: JSON) -> Decoded<[Child]?> {
let decoded: Decoded<[ChildDecoder]?> = json <||? key
return decoded.map { optional in optional.map { array in array.map { $0.child } } }
} But I'd love to avoid that if there's a way to make this work. |
What about replacing |
Nope.
|
Oh yeah, that'd need to use
Maybe? |
That seems to work! Is there a convenient way to leverage that for the |
a bit more complicated (and off the top of my head, so forgive slight syntax issues) but I think you can do that like:
|
and then obviously you could pull that into a helper function or something if you need to |
Sadly, that doesn't work. 😞
|
Sorry I ghosted on this. Messing around with this now, it seems like there's something weird going on with the array stuff, and is unrelated to the optionality of the result: let child: Decoded<Child?> = .optional(json <| "child" >>- Child.decode)
// ^^ fine
let children: Decoded<[Child]> = json <|| "children" >>- [Child].decode
// error: 'Child' is not convertible to 'Parent.DecodedType' (aka 'Parent') I'll admit, I'm a little lost on this one. I can't quite see what is happening here. |
@mdiep did you ever resolve this? |
No, I had to keep using this workaround. |
I'm trying to convert some code to use Argo, and I'm getting stuck where the existing code uses subclasses. I can't get type inference to pass.
If anyone knows how to get this to work, I'd love some help.
Error Messages
Models
Argo Version
Argo 4.1.2
Xcode 8.3.2
Dependency Manager
Carthage
The text was updated successfully, but these errors were encountered: