[swift-evolution] [Proposal] Set literal and Set type syntax

Jack Lawrence jackl at apple.com
Mon Jan 18 16:53:37 CST 2016


init(arrayLiteral:) is there to satisfy ArrayLiteralConvertible. Set([“a”, “b”, “c”]) works just fine.

Jack
> On Jan 18, 2016, at 2:50 PM, Liam Butler-Lawrence via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Set("a", "b", "c”) doesn’t compile. It currently has to be  Set(arrayLiteral: "a", "b", "c”). That said, I’d be satisfied with removing the external parameter name “arrayLiteral”. Not only is it unnecessary, but it’s confusing too: variadic parameters are not the same as an Array.
> 
> Liam
> 
>> On Jan 18, 2016, at 5:43 PM, Howard Lovatt via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Sorry -1 from me. Doesn't seem worth it. Not much wrong with Set("a", "b", "c").
>> 
>> On Tuesday, 19 January 2016, Seth Friedman via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> I dig the idea of Sets having a literal syntax, rather than continuing to be the ugly stepchild of CollectionTypes. I'm not sure, though, that this particular literal syntax is very obvious. While the Array and Dictionary syntaxes are similar enough to many other languages in which arrays and maps exist, I might be confused if I saw your proposed syntax in Swift. 
>> 
>> Not to mention that it's not the prettiest syntax, but that's more subjective.
>> 
>> On Mon, Jan 18, 2016 at 1:24 PM, Michael Henson via swift-evolution <swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
>> Swift currently has literal and type shorthand syntax for native Array and Dictionary types, but not the Set type. It would be useful to have a literal shorthand for Set as well.
>> 
>> The existing productions for array and dictionary literals and types share brackets as delimiters, differing only in the contents between the brackets. That poses a slight problem for Set because any syntax, to be useful, must also be easily distinguishable from the other types.
>> 
>> Consider that Arrays and Dictionaries are both naturally indexed collections. Arrays by the integer value of the order of items in the collection, usually implicitly, and Dictionaries by the hashed key associated with each value.
>> 
>> Arrays, implicit index:
>> 
>> let array = ["a", "b", "c"]
>> var array: [String]
>> var empty: [String] = []
>> 
>> Dictionaries, explicit index:
>> 
>> let dictionary = ["a": 1, "b": 5, "c": 9]
>> var dictionary: [String: Int]
>> var empty: [String: Int] = [:]
>> 
>> Sets, by contrast, have no particular order and no "key". Even though the Set is enumerable and iterable, it isn't indexed. With that in mind, we can declare that a Set literal or Set type literal should distinguish itself by declaring that it has no index.
>> 
>> The Set literal could be:
>> 
>> let set = [ _: "a", "b", "c" ]
>> var set = [ _: String ]
>> var empty: [ _: String ] = [_:]
>> 
>> In the grammar:
>> 
>> set-literal -> [ _ : array-literal-items[opt] ]
>> literal-expression -> array-literal | dictionary-literal | set-literal
>> 
>> set-type -> [ _ : type ]
>> type -> array-type | dictionary-type | set-type | ... etc.
>> 
>> 
>> Examples:
>> 
>> let x = [ _: "A", "B", "C" ]
>> let y: [ _: String ] = [ _: ]
>> 
>> 
>> Alternatives considered:
>> 
>> Without literals, declaring a Set type is straightforward, easy to recognize, and not much more verbose. There might not be enough of a difference to justify special syntax in the core language.
>> 
>> Mike
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> 
>> 
>> 
>> -- 
>> Seth Friedman
>> Software Development Engineer II
>> Amazon.com <http://amazon.com/>
>> 
>> -- 
>>   -- Howard.
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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


More information about the swift-evolution mailing list