Skip to content
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

SwiftyJSON Array extension #17

Closed
Nonouf opened this issue Sep 20, 2016 · 10 comments
Closed

SwiftyJSON Array extension #17

Nonouf opened this issue Sep 20, 2016 · 10 comments
Assignees

Comments

@Nonouf
Copy link

Nonouf commented Sep 20, 2016

Is there any reason why the array extension of JSONDecodable is commented since the last update? https://github.com/MLSDev/TRON/blob/master/Source/SwiftyJSON/SwiftyJSONDecodable.swift#L58

@DenTelezhkin
Copy link
Member

Yep. We simply cannot get it to work reliably.

What we want to do, is to create an extension like

extension Array : JSONDecodable where Element: JSONDecodable

However it's impossible in Swift 2 and Swift3. We can cast like this of course:

Element.self as? JSONDecodable.Type

However, it's an internal check, that produces unexpected results. It makes every array conform to JSONDecodable, whether you like it or not. We thought that for now we might comment this extension out, because we really want to get it to work properly, not hacky. But it seems before Swift 4 it's impossible, so at least we wanted to give our users a knowledge that this hack exists, and you can manually copy and paste it into your project if needed.

If you have any ideas how to make this extension work, i would be really happy to hear them, i've been struggling with this extension basically since the beginning of TRON development.

@Nonouf
Copy link
Author

Nonouf commented Sep 20, 2016

It makes sense. Thanks for the clarification and I understand your struggle.
I will try to think about it and see if I can come with a solution.

Meanwhile, could be great to leave a note on the README because I guess some people can be confused or won't understand why they can use all other types except Array.

@DenTelezhkin
Copy link
Member

Added information about this to readme, i'll leave this issue open for anybody interested(and hopefully find a way to close it with Swift 4)

@DenTelezhkin
Copy link
Member

Seems like we'll be revisiting this for Swift 3.1 🚀
swiftlang/swift#5126

@DenTelezhkin DenTelezhkin self-assigned this Dec 1, 2016
@ghost
Copy link

ghost commented Mar 22, 2017

@DenHeadless Any news that this will be fixed with 3.1?

@DenTelezhkin
Copy link
Member

I will look into it once final Xcode 8.3 lands, but from initial testing it seems that this issue will not be fixed. Swift 3.1 allows concrete constrained extensions, for example:

extension Array where Element == String

However what we need is something a little different, we need both protocol conformance and element protocol conformance:

extension Array : JSONDecodable where Element: JSONDecodable

@ghost
Copy link

ghost commented Mar 22, 2017

@DenHeadless Ok I will try to find an other way

@DenTelezhkin
Copy link
Member

This is now documented in Response Serializers.

@DenTelezhkin
Copy link
Member

Seems like this will be possible with Swift 4.1 protocol conditional conformances. https://github.com/apple/swift/pulls?utf8=✓&q=+conditional+conformances

@DenTelezhkin DenTelezhkin added this to the 5.0.0 milestone Dec 27, 2017
@DenTelezhkin DenTelezhkin removed this from the 5.0.0 milestone Apr 3, 2018
@DenTelezhkin
Copy link
Member

This is now implemented on master branch using Swift 4.1 conditional conformances.

Finally 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants