[swift-evolution] TreeLiteralConvertible

Andrey Tarantsov andrey at tarantsov.com
Fri Apr 15 02:24:52 CDT 2016


Hey!

>> Can you please give us a few real-world examples where initializing a nontrivial tree-like data structure in code would be useful?
> 
> I suppose we always prefer to move *all* data into databases or files with dedicated data formats, *including* arrays, strings, dictionaries, etc. Sure. But it would be rather underwhelming if you could not also just instantiate an array or a string from a literal.

> Likewise, I find it a little deflating that I cannot express a piece of JSON in code. 
> 
> Given the *simplicity* of these structures, it seems it should not be beyond Swift to represent them in code with ease and elegance. And to begin with, all we need are those couple of protocols.


Did you just... sidestep the question? :-)

The reason we want dictionary and array literals is that because they're needed in the real code bases, everywhere, not just for the abstract beauty of the langauge. I can certainly list many specific use cases for both array and dictionary literals, with code examples.

I can't think of any real use cases for the kind of literals you want, though, hence the question. (And I don't think it's worth adding just for computer science playground experiments alone.)

And, FWIW, JSON objects can absolutely be represented in Swift as Dictionary<String, AnyObject>. I even typealias that as JSONObject, and have a lot of code parsing and producing these. While TreeDictionary<String, String> looks superficially safer, I doubt it's worth the trouble. But again, I'd like to be proven wrong; the burden is on you to present the use cases!

I'm appalled by the fascination of Swift community with JSONValue-like enums. They produce the most horrible code on the parsing side, and seem to be an academic experiment in type safety. See this framework for what I believe to be the right approach to dealing with JSON in Swift: https://github.com/ExpressiveSwift/ExpressiveCasting

A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160415/3df0874e/attachment.html>


More information about the swift-evolution mailing list