[swift-evolution] Proposal: Tuple Convertible

Nikolai Vazquez nvazquez1297 at gmail.com
Sun Dec 6 17:40:10 CST 2015


When working with points, I’ll sometimes make a typealias to an integer
tuple:

typealias Point = (x: Int, y: Int)

However, if I want to add extended functionality to my Point type, I would
have to change it to a struct, removing the ability to create one on the
fly with a tuple.

I’m proposing a TupleConvertible protocol that allows initialization from a
tuple directly.

struct Point: TupleConvertible {

    var x, y: Int

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }

    init(tuple: (x: Int, y: Int)) {
        self.init(x: tuple.x, y: tuple.y)
    }

}

This would make it very easy to create new Point instances.

let somePoint: Point = (50, 120)let otherPoint = Point(x: 50, y: 120)

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/dd4af639/attachment.html>


More information about the swift-evolution mailing list