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

Adrian Zubarev adrian.zubarev at devandartist.com
Wed Nov 23 12:50:30 CST 2016


The general idea of a keyword vector kept me thinking for quite a while.

Could such a keyword potentially replace a Tuple?

let aTuple: (Int, Int, Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6,7,8)

let aVector: vector(8) Int = (1,2,3,4,5,6,7,8)

// or `Int vector(8)`
// or `vector(of: 8) Int`
// or `Int vector(of: 8)`
It’s way easier to write and it reads well to.

There are other potential designs vor such a keyword that I could think of:

#vector(Int, 8)
#vector(8, Int)
#vector(of: 8, Int)
Or we could combine the idea of vectors with tuples. By that I mean that we still should be able to write labeled tuples/vectors like:

typealias MyType = (a: Int, b: Int) // labeled `vector(2) Int`
As for variadic generics:

func foo<vector T>(a: vector(3) String, b: T) {
     
    print(a.0)
    print(a.1)
    print(a.2)
     
    print(b)
}
When using vectors inside the angle brackets, vector T represents an arbitrary number of n individual, independent type parameters such as T1, T2, … , Tn. And we also could limit the number of the generic parameters we want.

func boo<vector(2) T>(a: T) {
     
    // here we know the exact boundary and it's safe to use the index of the vector
    print(type(of: a.0)) // T1
    print(type(of: a.1)) // T2
    print(type(of: a.2)) // T3
}
We should also be able to define a variable boundary which can only be using in other vectors of the same scope.

let a: vector(8) Int = (1,1,1,1,1,1,1,1)
let b: vector(8) Int = (1,1,1,1,1,1,1,1)


// vectors can be passed by tuples/vectors
// here is `x` a variable boundary  
func add(a: vector(x) Int, b: vector(x) Int) -> vector(x) Int {
    return a + b
}

add(a: a, b: b) // returns (2,2,2,2,2,2,2,2)
The Swift community also want to extend tuples one day. We should be able to this with vectors as well:

extension (Int, Int) { … }

// VS:

extension vector(2) Int { … }
Vectors would fully eliminate that ugly … pre-/postfix and leave it only for ranges.



-- 
Adrian Zubarev
Sent with Airmail

Am 22. November 2016 um 12:49:39, Karl (razielim at gmail.com) schrieb:

I’m okay with prefix … in the generic parameter list, but I don’t think variable declarations should have any annotation at all. We don’t declare an array as `array var myList: String`.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161123/3b4d0793/attachment.html>


More information about the swift-evolution mailing list