[swift-evolution] [Discussion] What is the future of tuples in Swift?
Adrian Zubarev
adrian.zubarev at devandartist.com
Thu Mar 2 06:52:53 CST 2017
Actually the generics manifesto has a nice solution on how we could extend tuples in the future. Parameterized extensions + variadic generics could allow us a lot.
I played with a simple sketch on how it could be enhanced. Especially I personally like the ability of fixed vs. unbound parameter length.
Here is the sketch:
Vectors
A vector contains at least one type.
vector T - is a list of type T of an arbitrary length, such as T1, T2, …
vector(n) T - is a list with the length of maximal n T’s, such as T1, T2, … , Tn
// vectorized tuple with arbitrary length (like an existetial that can accept a tuple of type `T` of any length)
var t1: (vector Int) = (1, 2)
t1 = (1, 2, 3)
t1 = (1, 2, 3, 4)
// vectorized tuple of fixed length
let t2: (vector(10) Int) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
// replacing the variadic syntax `T...` with vectors
func foo1(_: vector Int)
func foo2(_: (vector Int))
func foo3(_: vector (vector Int))
foo1(1, 2, 3, 4)
foo2((1, 2, 3, 4, 5))
foo3((1, 2), (1, 2, 3, 4), (1, 2, 3))
// variadic with fixed length
func foo4(_: vector(3) Int)
func foo5(_: vector(3) (vector(3) Int))
foo4(1, 2, 3)
foo5((1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 4))
// `foo5` would be equivalent to `func foo5(_: (Int, Int, Int), _: (Int, Int, Int), _: (Int, Int, Int))`
func foo3(_: vector (vector Int))
Variadic generics
generic T - a constraint applied to T to indicated that T is not fixed and can be changed iteratively.
generic does not provide any information about the length of the parameter list.
// combined with vectors we'll get variadic generics
// the reason why we need both `vector` and `params` is to be able to tell the compiler that `T` has
// an arbitrary paramater length (vector) *and* that `T` can change iteratively (generic)
struct ZipSequence<vector generic T> : Sequence where T : Sequence { ... }
func zip<vector generic T>(_ sequences: vector T) -> ZipSequence<vector T> where T : Sequence { ... }
The generic keyword is more like a placeholder in that sketch.
Extending tuples:
// parameterized extensions
// vectorized tuples
extension<T> (vector T) : MutableCollection { ... }
// conditional conformances
extension<vector generic T> (vector T) : Equatable where T : Equatable { ... }
--
Adrian Zubarev
Sent with Airmail
Am 2. März 2017 um 12:24:03, Anton Zhilin (antonyzhilin at gmail.com) schrieb:
I think that tuples should remain what they are now. Static-length vectors should be types on their own and interact with tuples, with structs and with
Array<…> in the same way.
newtype should be what enables extension of tuples:
newtype Money = (Int, Int)
extension Money: CustomStringConvertible {
var description: String {
return String(format: "$%d.%02d", arguments: [getSelfFirst, getSelfSecond])
}
}
let x = (0, 42)
let y = x as Money // error
print(x.description) // error
let z = Money(0, 42)
print(z.description) //=> $0.42
Here,
getSelfFirst and
getSelfSecond are placeholders for some syntax to access the underlying type.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170302/398ae68d/attachment.html>
More information about the swift-evolution
mailing list