As someone who wants to be able to use tuples to express existing types, I'd want to be able to omit argument labels. I want it to look extremely clean, I'm not just trying to save 4-5 characters. What I really want is something like ExpressibleByArray/String/DictionaryLiteral
but for tuples:
protocol ExpressibleByTupleLiteral {
associatedtype TupleLiteralType
init(tupleLiteral tuple: Self.TupleLiteralType)
}
class Foo: ExpressibleByTupleLiteral {
typealias TupleLiteralType = (Int, Int)
let x: Int
let y: Int
init(tupleLiteral tuple: Self.TupleLiteralType) {
self.x = tuple.0
self.y = tuple.1
}
}
let f: Foo = (5, 6)
This is much more flexible than some specific syntax, like .(x: Int, y: Int)
and it would work for any type you'd want it to, not just structs. And it's opt-in. As another example of how flexible it could be:
class Person: ExpressibleByTupleLiteral {
typealias TupleLiteralType = (String, Foo)
let name: String
let f: Foo
let i: Int
init(tupleLiteral tuple: Self.TupleLiteralType) {
self.name = tuple.0
self.foo = tuple.1
self.i = 5
}
}
let bob: Person = ("Bob", (5, 6))
Our Person
wishes to give i
a default value, and we are free to omit it from our TupleLiteralType
. Also, since Foo
is also ExpressibleByTupleLiteral
, we can nest tuples and the inner tuple is inferred as a call to Foo(tupleLiteral: (5, 6)