<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Yes indeed, in that case please disregard that comment, I drew a mental blank for some reason!</div><div class=""><br class=""></div><div class="">Thinking about it I’m just not sure about a syntax specifically for sets. That said, I’m kind of interested by the idea of the proposed syntax in terms of being able to specify a type of index for any indexed type (with underscore indicating no index as proposed). For example:</div><div class=""><br class=""></div><div class="">let x:SomeIndexedType = [Int:1, 2, 3, 4] // An indexed collection where the index type is Int (e.g- a regular Array)</div><div class="">let x:SomeIndexedType = [UInt8: 1, 2, 3, 4] // Similar to above, but limited to Uint8 indexes the type is incapable of storing more than 256 elements, coincidentally if you convert your indices from a larger integer type by truncating then you get automatic round-robin storage.</div><div class="">let x = [_: 1, 2, 3, 4] // Set as a special case (otherwise indexed types would need to handle optional index types)</div><div class=""><br class=""></div><div class="">Given that the collection types are already written to use the index paradigm externally rather than just assuming the use of Int for indices, this could allow for some interesting possibilities from using custom index types, e.g- allowing an Array to store elements by alphabetic letters. Of course internally some changes might be needed as collections would need to use the distance from the start index to perform lookups, but for integer types this should optimise away I think, and shouldn't be a difficult change to make.</div><div class=""><br class=""></div><div class="">But yeah; for Sets only I’m not convinced the need is worth it given that array syntax works in most cases, but if the same syntax can be useful for a broader range of types it could be interesting.</div><br class=""><div><blockquote type="cite" class=""><div class="">On 19 Jan 2016, at 10:35, Johan Jensen via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Isn't that exactly what ArrayLiteralConvertible does?<br class=""><br class=""><span style="font-family:monospace,monospace" class="">extension CustomType: ArrayLiteralConvertible {<br class=""> typealias Element = Int<br class=""> init(arrayLiteral elements: Element...) {<br class=""> <br class=""> }<br class="">}<br class=""><br class="">let c: CustomType = [1,2,3,4]</span><br class=""><br class=""></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jan 19, 2016 at 10:18 AM, Haravikk via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Same here, as you long as you specify Set as a type then the standard array syntax works just fine, the only case in which it doesn’t work is when you want to do something like:</div><div class=""><br class=""></div><div class="">[1,2,3,4].someMethod()</div><div class=""><br class=""></div><div class="">But that isn’t usually a great way to use array/set constants anyway so I don’t think it’s a big deal.</div><div class=""><br class=""></div><div class="">A more interesting question IMO is whether we could extend the array syntax to apply to any sequence type, for example:</div><div class=""><br class=""></div><div class="">let mySequence:SomeProtocol = [1, 2, 3, 4]</div><div class=""><br class=""></div><div class="">i.e- could we add a protocol that Array and Set conform to in order to support that style of initialisation, that we could also apply to other types as well. Is that worth its own proposal?</div><div class=""><div class="h5"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 19 Jan 2016, at 01:43, zhaoxin肇鑫 via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">I choose let x: Set = [1, 2, 3, 4] // x inferred to be Set<Int>. The current way. Unless the output of print(a set) change its format.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">zhaoxin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jan 19, 2016 at 6:51 AM, Jack Lawrence via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It doesn’t seem like a big enough win over:<br class="">
<br class="">
let x: Set = [1, 2, 3, 4] // x inferred to be Set<Int><br class="">
<br class="">
Especially since sets are used so infrequently compared to Array and Dictionary.<br class="">
Jack<br class="">
<div class=""><div class="">> On Jan 18, 2016, at 1:24 PM, Michael Henson via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class="">
><br class="">
> 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.<br class="">
><br class="">
> 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.<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Arrays, implicit index:<br class="">
><br class="">
> let array = ["a", "b", "c"]<br class="">
> var array: [String]<br class="">
> var empty: [String] = []<br class="">
><br class="">
> Dictionaries, explicit index:<br class="">
><br class="">
> let dictionary = ["a": 1, "b": 5, "c": 9]<br class="">
> var dictionary: [String: Int]<br class="">
> var empty: [String: Int] = [:]<br class="">
><br class="">
> 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.<br class="">
><br class="">
> The Set literal could be:<br class="">
><br class="">
> let set = [ _: "a", "b", "c" ]<br class="">
> var set = [ _: String ]<br class="">
> var empty: [ _: String ] = [_:]<br class="">
><br class="">
> In the grammar:<br class="">
><br class="">
> set-literal -> [ _ : array-literal-items[opt] ]<br class="">
> literal-expression -> array-literal | dictionary-literal | set-literal<br class="">
><br class="">
> set-type -> [ _ : type ]<br class="">
> type -> array-type | dictionary-type | set-type | ... etc.<br class="">
><br class="">
><br class="">
> Examples:<br class="">
><br class="">
> let x = [ _: "A", "B", "C" ]<br class="">
> let y: [ _: String ] = [ _: ]<br class="">
><br class="">
><br class="">
> Alternatives considered:<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Mike<br class="">
</div></div><div class=""><div class="">> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>