You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
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.
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)?
Environment
macOS 10.13.3, Xcode 9.3b2, Swift 4.1 Snapshot 2018-02-06
Additional Detail from JIRA
md5: 625483a1447d19328cb1d74477d7b806
Issue Description:
Please consider the following example that pretty much defines a JSON hierarchy:
Which works fine, however, this dictionary literal fails:
Similarly:
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?The text was updated successfully, but these errors were encountered: