CTMacUser
(Daryle Walker)
February 9, 2020, 5:41pm
1
Continuing the discussion from Tuples conform to Equatable :
I know that functions that take an Optional
parameter can use a non-optional instance as an argument. Do tuples have the same relationship? I mean that is a tuple with at least one label member a sub-type of a tuple with the same shape but (at least) some labeled members become unlabeled? [...]
Is there such a relationship between tuple types? If not, should we add it? I think so.
cukr
February 9, 2020, 5:47pm
2
There is already such relationship, but it goes both ways, so we cannot say that labelled is a subtype of unlabelled, or vice versa
check out this code:
var labelled: (a: Int, b: Int) = (a: 1, b: 2)
var unlabelled: (Int, Int) = (3, 4)
labelled = unlabelled
unlabelled = labelled
// labelled = (c: 5, d: 6) doesn't compile
xwu
(Xiaodi Wu)
February 9, 2020, 5:55pm
3
CTMacUser:
Continuing the discussion from Tuples conform to Equatable :
I know that functions that take an Optional
parameter can use a non-optional instance as an argument. Do tuples have the same relationship? I mean that is a tuple with at least one label member a sub-type of a tuple with the same shape but (at least) some labeled members become unlabeled? [...]
Is there such a relationship between tuple types? If not, should we add it? I think so.
As I already replied in that thread, the relation between tuple types is as follows:
Right now, Swift's type system allows tuple conversions that both add and remove labels. These kinds of bidirectional subtyping rules are difficult for the type checker because it isn't clear which one is "better" or more specific. If we made the rule directional, to say that maybe (T, U) is a supertype of (label: T, label: U) and not the other way around, then the type checker would be able to clearly favor labeled tuples as more specific than unlabeled ones.