I’ve been wanting tuple-initializable in Swift for Core Graphics, where it’s a drag to always use long and cumbersome initializers when I’m not building production code:
let point: CGPoint = (100, 50)
Yeah, it is better form to use labeled initializers but I’m anyone using CGPoint
understands the correspondence between (x, y) coordinates. And I don’t want to just build CGPoint.init(_ x:, _ y:)
extensions. I like the tuple form.
Right now, the closest you can get is the silly:
let point = (CGPoint.init as (CGFloat, CGFloat) -> CGPoint)(100, 50)
And that’s (pardon the pun) pointless.
It would be pretty cool to allow memberwise tuples with or without labels in place of initializers when the tuple field types could be unambiguously determined:
let point: CGPoint = (x: 100, y: 50) // instead of: let point = CGPoint(x: 100, y: 50) let person: Person = ("Mary", nil, "Santos") // instead of: let person = Person(first: "Mary", middle: nil, last: "Santos")
It may be ugly but it would be hella useful in Playgrounds.
4 Comments
One idea I had to address this a while back was this: Just as .something is interpreted as static member of the contextual type, .(x: 2, y: 30) could be interpreted as an initialiser of the contextual type.
That’s actually kinda already the case, it’s just .init instead of just . 😉
For example you can already write this:
let point: CGPoint = .init(x: 2, y: 30)
Best workaround I found so far is:
Clearly not ideal, but ¯\_(ツ)_/¯
Instead of a tuple how about an array?