[swift-evolution] [Proposal][Discussion] Deprecate Tuple Shuffles
Xiaodi Wu
xiaodi.wu at gmail.com
Mon May 8 03:35:33 CDT 2017
On Mon, May 8, 2017 at 2:51 AM, AndrĂ© Videla <andre.videla at gmail.com> wrote:
> Let me show you:
>
> assume we have this data type which is just a pair of Ints
>
> enum Pair {
>
> case point(x: Int, y: Int)
>
> }
>
> and see how Swift allows us to deconstruct it:
>
> if case .point(let x, let y) = Pair.point(x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> this is perfectly fine. Even if the labels are omitted the structure is
> kept and x = 3 and y = 5
>
> if case .point(x: let x, y: let y) = Pair.point(x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> perfectly fine, x and y are given and correspond to the actual label of
> the enum case x = 3, y = 5
>
>
> if case .point(y: let x, x: let y) = Pair.point(x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> This is an error, as expected, labels do not correspond to any existing
> known structure and the match makes no sense. It does not compile
>
> Now we refactor the code a bit and we start using a pair instead of an
> enum case
>
> if case (let x, let y) = (x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> this is fine since the structure is preserved from the value on the right,
> x = 3, y = 5
>
> if case (x: let x, y: let y) = (x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> this is fine since the labels correspond to the existing structure on the
> right x = 3, y = 5
>
> if case (y: let x, x: let y) = (x: 3, y: 5) {
>
> print("\(x), \(y)")
>
> }
>
> And this.
>
> This compiles even though the *structure matched does not correspond to
> the structure of the value against which it matches*. x = 5, y = 3
>
Yes, and Robert is proposing to eliminate tuple shuffles from the language;
if accepted, then this last example will behave differently.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170508/6c74f983/attachment.html>
More information about the swift-evolution
mailing list