[swift-evolution] [Pitch] Require tuple conversions to be explicit when labels don't match
Jacob Bandes-Storch
jtbandes at gmail.com
Mon May 9 01:46:49 CDT 2016
There was some previous discussion under "[Discussion] Enforce argument
labels on tuples
<http://thread.gmane.org/gmane.comp.lang.swift.evolution/14910>".
Halfway through the thread, Haravikk clearly stated the key point:
On Thu, Apr 21, 2016 at 12:14 AM, Haravikk via swift-evolution <
swift-evolution at swift.org> wrote:
> I think the important thing to remember is that the label check is
> intended to prevent cases like this:
> let a:(left:Int, right:Int) = (1, 2)
> var b:(right:Int, left:Int) = a
> While the two tuples are compatible by type, the meaning of the values
> may differ due to the different labels; in this case the values are
> represented in a different order that a developer should have to explicitly
> reverse to ensure they aren’t making a mistake, or they could represent
> radically different concepts altogether.
I agree there's a potential for confusion here, and I suggest we should add
an error (or warning) imploring the user to make the conversion explicit,
when the source tuple is labeled:
func foo() -> (left: Int, right: Int) { return (3, 4) }
let (left: a, right: b) = foo() // ok, labels match
var x = 0, y = 0
(left: x, right: y) = (1, 3) // ok, source is unlabeled
let (c, d) = foo()
// error: conversion between tuple types '(left: Int, right: Int)' and
'(Int, Int)' requires explicit 'as' operator
// suggested fix: "let (c, d) = foo() as (Int, Int)"
let (right: e, left: f) = foo()
// error: conversion between tuple types '(left: Int, right: Int)' and
'(right: Int, left: Int)' requires explicit 'as' operator
// suggested fix: "let (right: e, left: f) = foo() as (right: Int,
left: Int)"
Thoughts?
Jacob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160508/2cb3cfa0/attachment.html>
More information about the swift-evolution
mailing list