Proposal: Tuple Convertible


(Nikolai Vazquez) #1

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)


#2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Nikolai Vazquez) #3

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@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(John McCall) #4

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.

I think this is an interesting idea, but I believe it’s not actually expressible in the current generics system without hard-coding the protocol to just 2-tuples.

John.

···

On Dec 6, 2015, at 4:07 PM, Nikolai Vazquez via swift-evolution <swift-evolution@swift.org> wrote:

On Sun, Dec 6, 2015, 7:01 PM Stephen Celis <stephen.celis@gmail.com <mailto:stephen.celis@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tal Atlas) #5

Overall this is an interesting idea but in practice I think what you’re
looking for is more of scala implicit conversions. A way to define a
conversion from one type to another so that when you cast the implicit
conversion gets run for you. It’s much more flexible and gives you what you
want without only working for tuples.

···

On Mon, Dec 7, 2015 at 1:02 AM John McCall via swift-evolution < swift-evolution@swift.org> wrote:

On Dec 6, 2015, at 4:07 PM, Nikolai Vazquez via swift-evolution < > swift-evolution@swift.org> wrote:

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.

I think this is an interesting idea, but I believe it’s not actually
expressible in the current generics system without hard-coding the protocol
to just 2-tuples.

John.

On Sun, Dec 6, 2015, 7:01 PM Stephen Celis <stephen.celis@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________

swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution