On Mon, May 8, 2017 at 2:51 AM, André Videla <andre.vid...@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.
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution