[swift-evolution] Synthesizing Equatable, Hashable, and Comparable for tuple types

Mike Kluev mike.kluev at gmail.com
Sat Nov 25 17:00:15 CST 2017


on Date: Thu, 23 Nov 2017 09:56:35 +1100 Howard Lovatt <
howard.lovatt at gmail.com> wrote:

>
> I would defend turning tuples into structs (change from structural type to
> nominal type). This is a much better story for programmers, compare the two
> stories:
>
>    1. Tuples are just syntax sugar for simple structs.
>    2. Tuples are sort of like structs but there is a list of things tuples
>    can do that structs can't and a list of things structs can do and tuples
>    can't.
>
> I think unification can be achieved with some name mangling (Chris Lattner
> noted this previously - I am just spelling out one scheme), e.g.:
>
> // var a = (zero: 0, one: 1)
> public struct Tuple_zero_Int_one_Int { // Mangle name.
>     public var zero: Int
>     public var one: Int
> }
> var a = Tuple_zero_Int_one_Int(zero: 0, one: 1)
> // a.0 = -1
> a.zero = -1
>
> // var b = (0, 1)
> public struct Tuple_0_Int_1_Int { // Mangle name.
>     public var _0_: Int // Unique name.
>     public var _1_: Int // Unique name.
> }
> var b = Tuple_0_Int_1_Int(_0_: 0, _1_: 1)
> // a = b
> a = Tuple_zero_Int_one_Int(zero: b._0_, one: b._1_)
>
>
> Implicit in the above transformation is:
>
>    1. struct and tuple have the same memory layout.
>    2. `.0` access the 1st stored property of a struct, `.1` the 2nd, etc.
>

not sure about the name mangling per se, but the very idea of treating
tuples as structs is awesome and worth exploring.

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171125/44fbccd1/attachment.html>


More information about the swift-evolution mailing list