[swift-evolution] [Discussion] Parameter `vector` keyword vs. triple dot prefix for variadic generics

Adrian Zubarev adrian.zubarev at devandartist.com
Tue Nov 22 04:08:28 CST 2016

Hello folks,

I’d like to revive the discussion about the Variadic generics feature from the generics manifesto.

There exist an older draft proposal from Douglas Gregor and Austin Zheng.

As part of this discussion I also would like to talk about the possibly of a new enum type Wrapped (some of you might be familiar with Result or Either type). To be clear about this, I do not want to introduce a union type here, because if you might not know:

Disjunctions (logical ORs) in type constraints: These include anonymous union-like types (e.g. (Int | String) for a type that can be inhabited by either an integer or a string). “[This type of constraint is] something that the type system cannot and should not support.”

Source: Commonly Rejected Changes
But I still have hope for the infix | type operator as part of the Wrapped enum.

To beginn with, I’d like to thank to Douglas and Austin for their work back then and putting this draft together. To be honest while reading the proposal I found it hard to keep track of where the prefix … and where the postfix … was used. Especially it was really confusing to understand the behavior of each … operator.

Let’s look at the following 3 examples from the proposal:

struct Foo<...C : Collection> {
    // Indices and C are bound to be the same length.
    typealias ...Indices = C.Index...

    // A value vector of indices
    var (...startIndices): (Indices...)

    init(...x: C...) {
        startIndices... = x.startIndex...

struct Foo<...T : CustomStringConvertible> {

    var (...descriptions): (String...)

    init(...x: T) {
        // .description goes from Tn -> String
        // Because of this, descriptions' length is identical to that of T.
        descriptions... = x.description...

struct Foo<...T : CustomStringConvertible> {

    var (...descriptions): (String...)

    mutating func doSomething() {
        descriptions... = ("foo", "bar", "baz")
The first problem I realize here, is that the ... prefix has no ability to describe its own boundary and therefore the last example is not possible.

As contrast to this I’d like to pitch a new keyword vector and vector(Boundary_Size) (we could also use params instead, like in C#). Without the explicit Boundary_Size, which could be from 1 to (positive) n, the vector is unbound and acts exactly like the proposed ... prefix.

Let’s rewrite and reposition a few things from above with the new keyword:

struct Foo<vector C : Collection> {

    // Indices vector is bound to be the same length as its parent scope C vector, because we're using its `Index` here.
    typealias vector Indices = vector C.Index

    // A value vector of indices
    // Note: We *might* omit `vector` here for the value, because its type
    // already is a vector, or we want to be explicit everywhere?!
    vector var startIndices: Indices
    // Alternative 1 (preferred):
    var startIndices: Indices
    // Alternative 2 (still okay):
    var startIndices: vector Indices
    // Alternative 3 (really strange):
    vector var startIndices: vector Indices

    // Same here, `C` is a vector, so we could omit `vector` keyword before x
    init(vector x: C) {
        // Since the vectors `startIndices` and `x` are bound by a  
        // unbound *parent scope vector* we don't need the postfix `...`
        self.startIndices = x.startIndex
    // Alternative 1:
    init(x: C) {
        self.startIndices = x.startIndex
    // Alternative 2:
    init(x: vector C) {
        self.startIndices = x.startIndex

struct Foo<vector T : CustomStringConvertible> {

    // When there is no other context than the unbound parent scope vector,
    // the following vector will have the same length, otherwise it will be unbound.
    var descriptions: vector String

    init(x: T) {
        // .description goes from Tn -> String
        // Because of this, descriptions' length is identical to that of T.
        self.descriptions = x.description

// Allowed now
struct Foo<vector T : CustomStringConvertible> {

    // This vector has an explicit boundary size and therefore not bound  
    // to vector T
    vector(3) var descriptions: String
    // Alternative:
    var descriptions: vector(3) String

    mutating func doSomething() {
        // Assigning can be made with tuples
        self.descriptions = ("foo", "bar", "baz")
From my personal point of view this new approach is way easier to read. A really good thing about it, is that it eliminates the ugly pre-/postfix ... confusion.

Now let’s scratch the surface of the mentioned Wrapped enum.

Recently I bumped into an issue where no Swift feature could nicely solve it for me, not even conditional conformances with it’s full power. The only possible solution would be such a Wrapped enum (the only one in the whole language) or the ability to implicitly wrap any instances into enum cases when it’s possible.

// Implicitly wrapping

enum MyEnum {
    case string(String)
    case integer(Int)

let int = 42

// Matched against the cases and wrapped if possible, otherwise it'll be an error
let e1: MyEnum = int

// There is no `ExpressibleByStringLiteral` here, so the literal fallback to  
// its default type `String`, than matched against all the cases and wraps into `.string(String)`
let e2: MyEnum = "Swift"    

// Wrapped enum, with the same functionality as above.
enum Wrapped<vector T> {

    // How about the ability of extracting the vector index?
    vector case vector.index(T)
    // Alternative
    vector case #(T)

let e3: Wrapped<String, Int> = 0 // implicitly wrapped into `.1(Int)`

switch e3 {

case .0(let stringValue):
case .1(let intValue)

// Yes, this is not a typo but a possible useless artifact.
// For the Wrapped enum we could check for same types (probably very complex to achieve), or just leave it as is.
let e4: Wrapped<String, String> = "test"

switch e3 {

case .0(let stringValue):
case .1(let stringValue)
    // Will never print
As mentioned above I’d like to use the infix | type operator for the Wrapped enum as a shortcut.

let x: Wrapped<A, B, C> = ...
let x: A | B | C = ...

// x.0(A)
// x.1(B)
// x.2(C)
Any feedback is much appreciated. :)

Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161122/d9e8a4a6/attachment-0001.html>

More information about the swift-evolution mailing list