[swift-evolution] [Proposal][Discussion] Deprecate Tuple Shuffles
Xiaodi Wu
xiaodi.wu at gmail.com
Mon May 8 02:28:23 CDT 2017
On Mon, May 8, 2017 at 2:15 AM, David Hart <david at hartbit.com> wrote:
>
> On 8 May 2017, at 09:09, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
> On Mon, May 8, 2017 at 12:16 AM, David Hart <david at hartbit.com> wrote:
>
>>
>> On 7 May 2017, at 00:21, Xiaodi Wu via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>> To which human would it be misleading?
>>
>> To the writer? No, because the compiler will warn you right away. By the
>> time you're done with writing the first line, it'll warn you that Int and
>> Double are unused variables. And if you try to use x and y, you get an
>> error.
>>
>> To the reader? Only if the writer knowingly wrote this misleading code.
>> In other words, it's a nice puzzle, but no reader will encounter this in
>> real-world code, unless they're being tormented by the writer on purpose.
>>
>>
>> IMHO, the fact that the compiler warns you does no change the fact that
>> it's a very confusing part of the language. It should not be an excuse for
>> fixing it. Consistency teaches us to expect a type after a colon.
>>
>
> Highly disagree. Consistency teaches us that variables can come after a
> colon.
>
>
> Correct. I meant to say that sentence in the context of the LHS of an
> assignment.
>
As Adrian shows, enum case patterns have the exact same syntax during
pattern matching, and you'll find the variable that's being bound written
after a colon on the LHS:
```
enum Foo { case bar, baz(boo: Int) }
let foo = Foo.baz(boo: 42)
if case let .baz(boo: x) = foo {
print(x) // Prints "42".
}
```
There's a longstanding complaint that pattern matching in Swift is
backwards in terms of what's on the LHS and what's on the RHS. Recall the
numerous proposals on the list to introduce a `=~` operator that reverses
LHS and RHS as compared to the current `~=`. (IIRC, the core team responded
to say that they had tried it both ways and the current sequence was the
more ergonomic of the two.) But, in any case, with respect to consistency
with the rest of the language, tuple patterns with labels are *supremely*
consistent.
Can’t we even agree that the syntax is misleading?
>
No! Until this conversation I would have laughed at the suggestion that
*anyone* would find this even slightly misleading. Yes, Robert came up with
a very clever puzzle. I'll bet you there are *plenty* of neat puzzles that
you can come up with if you start naming your variables `Int` and `Double`.
That does _not_ demonstrate that tuple patterns are misleading.
> ```
> func foo(bar: Int, baz: Int) {
> print(bar, baz)
> }
>
> func foo(bar: Bool, baz: Bool) {
> print(bar, baz)
> }
>
> let x = 42
> let y = 43
> foo(bar: x, baz: y)
> // x and y are variables, and they come after the colon.
> // We don't allow users to write `foo(bar x: Int, baz y: Int)`,
> // even when there are overloads.
> ```
>
> There is nothing inconsistent about this part of the language.
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170508/a92b61b4/attachment.html>
More information about the swift-evolution
mailing list