JSON is a standard, popular, structured data format. The fact that JSON objects cannot be directly expressed in swift source code is a detriment, as it requires run-time evaluation. The compiler should be able to produce an object (probably of [String:Any]) at compile time. Both JavaScript and Python can take data structures that are JSON (or in the case of Python, sufficiently close as to not require reformatting). Additionally, extending past JSON, into JS or Python, defining a lamba function would be extremely useful.
For example in JS:
x = {
"func": function (a,b) {
return a+b
}
}
x["func"](1,2) -> 3
Swift already allows this, albeit not in precisely a JSON format. It also already supports the lambda functions you referenced:
let x: [String: Any] = [
"func": { (a: Int, b: Int) -> Int in
return a + b
},
"value": 231
]
let f = x["func"] as! (Int, Int) -> Int
print(f(1, 2)) // 3
The dynamicMemberLookup attribute was actually added in Swift with the motivation of working with dynamically-typed languages (such as Python and Javascript).
Though when using Any as a dynamic member result type, you lose type-inference (you still need to typecast with as? or as! each time). And with string-based keys, you lose autocompletion.
Also, there's ExpressibleByDictionaryLiteral, for when you want to add JSON-like initialization to existing types.
What is your motivation? Are you looking to be able to copy JSON and paste it directly into Swift? Are you generating Swift source code with embedded data from a JSON source? Do you just prefer JavaScript’s use of symbols to Swift’s?
Yes, being able to speak the data lingua franca of the internet transparently is a huge benefit. I am currently maintaining a multi-platform product, and I could just copy and paste these in on the other platforms, but with swift I have to do a lot of additional syntax conversion, which means now it has to be maintained separately.
I have a table large table that maps to values and functions, and while this is do-able in Swift It's a big table that now has to be maintained separately. I can't just encode it because it contains functions (so it is not true JSON) but I the lambdas simple enough to work on all platforms.
I was hoping Swift would allow { } for dictionaries (a shorthand for [String:Any]?) which would accomplish a lot of the syntax conversion that I am doing, though not entirely.
But as someone pointed out above (by providing the Swift version of the lambda) , the conversion of the lambda syntax is not portable.
So I guess this is a dumb idea.
But your responses have clued me into some areas I need to learn more. So it's not a total waste.
I agree with @brandon. Maybe you can do something like this:
var obj = (a: 1, b: (b1: someObj.prop))
No part in the dictionary has the same type, a is an Int, b is an object, and b.b1 is... something. It's totally not a job for Dictionary. It's a job for an instance, like class, struct, or non-nominal struct (tuple). I doubt any of them would gain new syntax just to match JSON, which already sounds like a non-goal.