[swift-evolution] Proposal: Tuple Convertible

Nikolai Vazquez nvazquez1297 at gmail.com
Sun Dec 6 18:07:20 CST 2015


I was thinking it could work for variables that are already tuples but then
that goes against Swift's strict typing. So really it should only be for
tuple literals.

On Sun, Dec 6, 2015, 7:01 PM Stephen Celis <stephen.celis at gmail.com> wrote:

> Do you mean a TupleLiteralConvertible? I’m also in favor of a
> ClosureLiteralConvertible (or FunctionLiteralConvertible).
>
> Stephen
>
> On Dec 6, 2015, at 6:40 PM, Nikolai Vazquez via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> 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)
>
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/9c1f4b4a/attachment.html>


More information about the swift-evolution mailing list