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

[SR-6941] Conditional Conformance + Literals #49489

Closed
swift-ci opened this issue Feb 7, 2018 · 4 comments
Closed

[SR-6941] Conditional Conformance + Literals #49489

swift-ci opened this issue Feb 7, 2018 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 7, 2018

Previous ID SR-6941
Radar None
Original Reporter charlieMonroe (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.13.3, Xcode 9.3b2, Swift 4.1 Snapshot 2018-02-06

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 625483a1447d19328cb1d74477d7b806

Issue Description:

Please consider the following example that pretty much defines a JSON hierarchy:

protocol JSONValue {}

extension String: JSONValue {}
extension Int: JSONValue {}
extension Array: JSONValue where Element: JSONValue {}
extension Dictionary: JSONValue where Key == String, Value: JSONValue {}

/** NSNull, NSArray, ... */

typealias JSONDictionary = [String : JSONValue]

Which works fine, however, this dictionary literal fails:

let dict: JSONDictionary = [
     "Hello": [1, "Joe"]
]

// Error: Contextual type 'JSONValue' cannot be used with array literal

Similarly:

let arr: [JSONValue] = ["1", 2, 4]
let dict: JSONDictionary = [
    "key": arr
]

// Error: value of type '[JSONValue]' does not conform to expected dictionary value type 'JSONValue'

In both cases, the fix is to add as JSONValue to the row, but I don't think this should be necessary and the conformance check should be automatic. Or am I mistaken?

@belkadan
Copy link
Contributor

belkadan commented Feb 7, 2018

I think this is correct. You've said "Array conforms to JSONValue when its element type conforms to JSONValue", but in this case the element type is JSONValue, which does not itself conform to JSONValue. Unfortunately, Swift only allows you to have one or the other; either [JSONValue] can conform to JSONValue or [String] and [Int] can conform to JSONValue, but not both.

@jckarter, anything to add?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Feb 8, 2018

Comment by Charlie Monroe (JIRA)

Thanks for the explanation. My major confusion was caused by the ObjC bridge - as I was initially hoping to get this working for validation of JSON objects without realizing that dynamically querying conditional conformance is not supported yet, I had added conformance to JSONValue to the Cocoa array and dictionary classes which resulted in:

[1, "Joe"] is JSONValue // warning: Test always OK
let arrObj: JSONValue = [1, "Joe"] // error: Contextual type 'JSONValue' cannot be used with array literal

Which was very confusing at the moment. However, it turns out that the is check used the ObjC bridge for the test as removing conformance of the NS* classes removed the always-OK warning.

@huonw
Copy link
Mannequin

huonw mannequin commented Mar 22, 2018

With #15415 this now gives an error that more directly highlights the underlying problem (https://bugs.swift.org/browse/SR-55):

conditional_conformance_as.swift:29:12: error: value of type '[JSONValue]' does not conform to expected dictionary value type 'JSONValue'
  "Hello": [1 as JSONValue, "Joe"]
           ^~~~~~~~~~~~~~~~~~~~~~~
                                   as! JSONValue

As such, I'm going to call this resolved; what do you think charlieMonroe (JIRA User)?

@swift-ci
Copy link
Collaborator Author

Comment by Charlie Monroe (JIRA)

Thanks, @huonw!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants