<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="">Thanks for getting the discussion started with such a thorough initial writeup.</div><div class=""><br class=""></div><div class="">My overall concern is this:</div><div class=""><br class=""></div><div class="">- on the one hand, I *very much* understand—and support—the idea of starting simple and adding functionality later</div><div class="">- on the other hand, this is *already* a rather complicated proposal syntactically, semantically, and probably also in implementation</div><div class="">- on the gripping hand, I’m concerned that despite being so complicated, it’s perhaps too limited to pay for itself as-proposed</div><div class=""><br class=""></div><div class="">Or, put differently, if you believe there’s an optimal point on the complexity/functionality for a v1, I worry a bit after reading it that it’s buying too little practical functionality for the complexity it’ll bring, and that something a *little* more complex might provide a *lot* more practical utility.</div><div class=""><br class=""></div><div class="">To that end, I’ve provided some concrete-ish examples of things it’d be nice to be able to do via variadics and the additional features they would either *need* or at least *benefit* from having available.&nbsp;</div><div class=""><br class=""></div><div class="">All of these are very much “nice to have” features that need not be included in a first version, but they each illustrate one possible complexity/capability trade-off.</div><div class=""><br class=""></div><div class="">A.1: Support for uniform variadic-parameter associated-type constraints?</div><div class=""><br class=""></div><div class="">Consider trying to write a variadic form of this sequence:</div><div class=""><br class=""></div><div class="">&nbsp; // given sequences a, b, provides a sequence equivalent-to</div><div class="">&nbsp; // zip(a,b).lazy.map() {&nbsp;</div><div class="">&nbsp; // &nbsp; ( min($0.0,$0.1), max($0.0,$0.1) )</div><div class="">&nbsp; // }</div><div class="">&nbsp; struct ExtremaSequence&lt;A:Sequence,B.Sequence where A.Iterator.Element == B.Iterator.Element, A.Iterator.Element: Comparable&gt; {</div><div class="">&nbsp; &nbsp; private let a: A; private let b: B</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; struct ExtremaIterator&lt;A:Iterator,B.Iterator where A.Element == B.Element, A.Element:Comparable&gt; {</div><div class="">&nbsp; &nbsp; private var a: A?</div><div class="">&nbsp; &nbsp; private var b: B?</div><div class="">&nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; mutating func next() -&gt; (A.Element,A.Element)? {</div><div class="">&nbsp; &nbsp; &nbsp; guard let nextA = a?.next() else {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; a = nil; b = nil;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return nil</div><div class="">&nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; guard let nextB = b?.next() else {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; a = nil; b = nil;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return nil</div><div class="">&nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; if nextA &lt; nextB {&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return (nextA,nextB)</div><div class="">&nbsp; &nbsp; &nbsp; } else {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return (nextB,nextA)</div><div class="">&nbsp; &nbsp; &nbsp; } &nbsp;</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">…would it be necessary to write the type’s generic parameters like this:</div><div class=""><br class=""></div><div class="">&nbsp; struct VariadicExtremaSequence&lt;E:Comparable,S… where S:Sequence, S.Iterator.Element &nbsp;== E&gt;</div><div class=""><br class=""></div><div class="">…or would there be some way of writing a constraint like “all `S.Iterator` must have same `.Element`?”?</div><div class=""><br class=""></div><div class="">A.2: Support for non-uniform "parameter-to-parameter” constraints</div><div class=""><br class=""></div><div class="">As an example:</div><div class=""><br class=""></div><div class="">&nbsp; protocol ValueTransformer {</div><div class="">&nbsp; &nbsp; associatedtype Input</div><div class="">&nbsp; &nbsp; associatedtype Output</div><div class="">&nbsp; &nbsp;</div><div class="">&nbsp; &nbsp; func transformedValue(for input: Input) throws -&gt; Output</div><div class=""><br class=""></div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; // how to express this (if possible), mock syntax below</div><div class="">&nbsp; struct LoggingValueTransformerApplier&lt;</div><div class="">&nbsp; &nbsp; T:ValueTransformer</div><div class="">&nbsp; &nbsp; where</div><div class="">&nbsp; &nbsp; …T[i],T[j]… =&gt; T[i].Output == T[j].Input&gt; : ValueTransformer {</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;typealias Input = #first(T…).Input</div><div class="">&nbsp; &nbsp;typealias Output = #last(T…).Output</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;private let (transformers) = (T...)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;func transformedValue(for input: Input) throws -&gt; Output {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;// evaluate incrementally, logging input =&gt; (output | error) step-by-step</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">…something like the above is definitely a "nice to have", but not having it will close off certain use cases.</div><div class=""><br class=""></div><div class="">B: Integer-Based Indexing</div><div class=""><br class=""></div><div class="">I see it’s been brought up already, but I’d *strongly* suggest making support for integer-based “indexing” into variadic packs a priority.</div><div class=""><br class=""></div><div class="">What concerns me is that without support for that, I suspect a lot of code would have to get "written twice” if using the “recursive” API on parameter packs.</div><div class=""><br class=""></div><div class="">Here’s a mock example:</div><div class="">&nbsp;&nbsp;</div><div class="">&nbsp; // a “weaker” dictionary-like protocol</div><div class="">&nbsp; protocol LookupTable {</div><div class="">&nbsp; &nbsp; associatedtype Key: Hashable</div><div class="">&nbsp; &nbsp; associatedtype Value</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; subscript(key: Key) -&gt; Value? { get } &nbsp; &nbsp;</div><div class=""><br class=""></div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; /// Does a cascading search for values through a possibly *heterogeneous*</div><div class="">&nbsp; /// collection of backing lookup tables…caching results to avoid subsequent searches</div><div class="">&nbsp; struct HeterogeneousCascadingLookupTable&lt;K:Hashable,V,T…&nbsp;</div><div class="">&nbsp; &nbsp; where&nbsp;</div><div class="">&nbsp; &nbsp; T:LookupTable,</div><div class="">&nbsp; &nbsp; T.Key == K,</div><div class="">&nbsp; &nbsp; T.Value == V&gt; : LookupTable {</div><div class=""><br class=""></div><div class="">&nbsp; private let (tables): (T…)</div><div class="">&nbsp; private var valueCache: [K:V] = [:]</div><div class="">&nbsp; private var missingKeys: Set&lt;K&gt; = []</div><div class=""><br class=""></div><div class="">&nbsp; // implementation *with* integer-based indexing:&nbsp;</div><div class="">&nbsp; subscript(key: K) -&gt; V? {</div><div class="">&nbsp; &nbsp; get {</div><div class="">&nbsp; &nbsp; &nbsp; guard !missingKeys.contains(key) else { return nil }</div><div class="">&nbsp; &nbsp; &nbsp; if let cached =&nbsp;valueCache[key] { return cached }</div><div class="">&nbsp; &nbsp; &nbsp; for index in 0..&lt;#count(T…) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if let v = tables[index][key] {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;valueCache[key] = v</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return v</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="">&nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; missingKeys.insert(key)</div><div class="">&nbsp; &nbsp; &nbsp; return nil</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; // implementation without integer-based indexing (or equivalent mechanism):</div><div class=""><div class="">&nbsp; subscript(key: K) -&gt; V? {</div><div class="">&nbsp; &nbsp; get {</div><div class="">&nbsp; &nbsp; &nbsp; guard !missingKeys.contains(key) else { return nil }</div><div class="">&nbsp; &nbsp; &nbsp; if let cached =&nbsp;valueCache[key] { return cached }</div><div class="">&nbsp; &nbsp; &nbsp; if let value = self.lookupValue(for: key, in: self.tables) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; valueCache[key] = value</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return value</div><div class="">&nbsp; &nbsp; &nbsp; } else {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; missingKeys.insert(key)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; return nil</div><div class="">&nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; }</div></div><div class=""><br class=""></div><div class="">&nbsp; // recursive lookup implementation (necessary b/c our type itself</div><div class="">&nbsp; // isn’t defined by recursively-nesting itself)</div><div class="">&nbsp; private final func lookupValue&lt;U…&nbsp;</div><div class="">&nbsp; &nbsp; where&nbsp;</div><div class="">&nbsp; &nbsp; U…: LookupTable,</div><div class="">&nbsp; &nbsp; U.Key == K,</div><div class="">&nbsp; &nbsp; U.Value == V&gt;(for key: K, in tables: U…) -&gt; V? {</div><div class="">&nbsp; &nbsp; return #first(tables)[key] ?? self.lookupValue(for: key, in: #rest(tables))</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">…which isn’t the end of the world, but having to write a separate recursive implementation of each method that needs to step-through the variadic parameters would spread things out a bit, it’d seem.</div><div class=""><br class=""></div><div class="">(Yes, this specific thing could be written today w/out variadics, but it illustrates the tradeoff here).</div><div class=""><br class=""></div><div class="">C: “Variadic-Sum” / Structural-Union?</div><div class=""><br class=""></div><div class="">I mentioned this before in a response to the manifesto email, but will reiterate it here: there’s a fair amount of useful things you could do with variadic generics *if* there was also some similar way to get a “structural union”.</div><div class=""><br class=""></div><div class="">A simple motivating example is something like a `Chain` sequence (analogous to `Zip`); e.g. something for which `chain(a,b)` is the sequence of ‘the elements in `a`, then the elements in `b`.</div><div class=""><br class=""></div><div class="">Although the proposed variadics make the sequence itself easy to write:</div><div class=""><br class=""></div><div class="">&nbsp; // for now I'd assume we need the `E` as a separate type parameter here,</div><div class="">&nbsp; // but recall the previous point</div><div class="">&nbsp; struct ChainSequence&lt;E,S… where S:Sequence, S.Iterator.Element == E&gt; {</div><div class="">&nbsp; &nbsp; private let (…sequences): (S…)</div><div class="">&nbsp; &nbsp; init(…arguments: S…) { … } &nbsp;</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">…the iterator is a bit more complicated. Without “structural unions” you’d probably wind up with something like this for a variadic implementation:</div><div class=""><br class=""></div><div class="">&nbsp; struct ChainSequenceIterator&lt;E,I… where I:Iterator, I.Element == E&gt; {</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; private var (…iterators): (I?…) // &lt;- I hope this works</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; mutating func next() -&gt; E? {</div><div class="">&nbsp; &nbsp; &nbsp; // find first non-nil iterator, try to get next element from it;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; // if next is non-nil, return that, otherwise nil it out and</div><div class="">&nbsp; &nbsp; &nbsp; // find the *next* non-nil iterator, etc….</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">…which could be made to work, but is wasteful in some ways (you have space to store n iterators…). You can make it a little cleaner if you have some way of doing integer-based indexing into the variadic parameters, but that doesn’t change the space requirements (it’s still going to need room for all `n` iterators + bookkeeping).</div><div class=""><br class=""></div><div class="">If, however, you have some variadic-compatible structural unions, you can write it as (roughly):</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; struct ChainSequenceIterator&lt;E,S… where S:Sequence, S.Iterator.Element == E&gt; {</div></div><div class="">&nbsp; &nbsp; &nbsp;private let source: ChainSequence&lt;E,S…&gt;</div><div class="">&nbsp; &nbsp; &nbsp;private var iterator: (S.Iterator |…) // (meant to be ~ `(S1.Iterator | S2.Iterator | S3.Iterator | …. | SN.Iterator)` )</div><div class="">&nbsp; &nbsp; &nbsp;private var done: Bool = false</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; &nbsp;mutating func next() -&gt; E? {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;// if not `done`, try `next` on current iterator in `iterator`</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;// if non-nil, return, otherwise advance to next iterator, etc., ...</div><div class="">&nbsp; &nbsp; &nbsp;}</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">…(how much space this actually saves depends on the size of `source` vs the size of the iterators, but it’s *possible* to save space this way).&nbsp;</div><div class=""><br class=""></div><div class="">Variadic generics *can* be added w/out structural unions — they’re a pure "nice-to-have" — but having support for variadic "product types" w/out corresponding variadic "sum types" is going to complicate-or-prevent certain constructs.</div><div class=""><br class=""></div><div class="">D: Make Parameter-Packs Named</div><div class=""><br class=""></div><div class="">I understand the appeal of the `…T`-style syntax, but I’d at least consider giving parameter-packs a more-concrete existence, e.g. something like this:</div><div class=""><br class=""></div><div class="">&nbsp; // strawman declaration syntax:</div><div class="">&nbsp; struct Foo&lt;I,J,K#ParameterPack&gt; {</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; // `K` here will refer to the variadic parameter-pack itself;</div><div class="">&nbsp; &nbsp; // cf the `typealias` below , but basically K ~ the tuple of its types</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; typealias KValueTuple = #tuple(K) // == tuple of values of type K.0, K.1, etc.)</div><div class="">&nbsp; &nbsp; typealias KTypeTyple - #typeTuple(K) // == tuple of types like K.0, K.1</div><div class="">&nbsp; &nbsp; typealias FirstK = #first(K)&nbsp;</div><div class="">&nbsp; &nbsp; typealias LastK = #last(K)&nbsp;</div><div class="">&nbsp; &nbsp; static var kArity: Int { return #count(K) }&nbsp;</div><div class="">&nbsp; &nbsp; // and so on</div><div class=""><br class=""></div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; // straw man point-of-use syntax, can be adjusted of course:</div></div><div class="">&nbsp; let concreteFoo = Foo&lt;I,J,K:(K0,K1,K2,…,Kn)&gt;</div><div class=""><br class=""></div><div class="">…which complicates the grammar, but IMHO feels a lot nicer than having a lot of "implicit rules" about what `…` means and so on.</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On May 28, 2016, at 3:03 PM, Austin Zheng via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello swift-evolution,<div class=""><br class=""></div><div class="">I put together a draft proposal for the variadic generics feature described in "Completing Generics" as a major objective for Swift 3.x. It can be found here:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/austinzheng/swift-evolution/blob/az-variadic-generics/proposals/XXXX-variadic-generics.md" class="">https://github.com/austinzheng/swift-evolution/blob/az-variadic-generics/proposals/XXXX-variadic-generics.md</a><br class=""></div><div class=""><br class=""></div><div class="">It adopts the syntax and semantics that are described in Completing Generics, and attempts to remain as simple as possible while still being useful for certain use cases (although I think there is still room to simplify). The proposal contains sample implementations for four use cases:</div><div class=""><br class=""></div><div class="">- Arbitrary-arity 'zip' sequence</div><div class="">- Arbitrary-arity tuple comparison for equality</div><div class="">- Tuple splat/function application</div><div class="">- Multiple-arity Clojure-style 'map' function</div><div class=""><br class=""></div><div class="">There is a lot of scope for design refinements, and even for alternative designs. With enhanced existentials, there was already an informal consensus that the feature would involve composing some protocols and class requirements, and placing constraints on the associated types, and most everything else was working out the various implications of doing so. That's not true for this feature.</div><div class=""><br class=""></div><div class="">In particular, I'm interested to see if there are similarly expressive designs that use exclusively tuple-based patterns and no parameter packs. I think Rust once considered a similar approach, although their proposal ended up introducing a parameter-pack like construct for use with fn application:&nbsp;<a href="https://github.com/rust-lang/rfcs/issues/376" class="">https://github.com/rust-lang/rfcs/issues/376</a></div><div class=""><br class=""></div><div class="">Feedback would be greatly appreciated. Again, be unsparing.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div><div class=""><br class=""></div></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>