[swift-evolution] [Discussion] Enforce argument labels on tuples
Chris Lattner
clattner at apple.com
Sun Apr 24 16:52:17 CDT 2016
On Apr 20, 2016, at 10:18 PM, David Owens II <david at owensd.io> wrote:
>>> Obviously, given a name to the parameter brings clarity and can be self documenting, but if we want the above to work while making names just as vital as the type signature, then we need to declare `Functor` as such:
>>>
>>> typealias Functor = (_ left: Int, _ right: Int) -> Int
>>>
>>> However, that's not even legal code today, and even if it were, is that really better?
>>
>> I don’t think this follows, since operator parameters are always unlabeled. I suspect we don’t reject it, but I’d be in favor of rejecting:
>>
>> func +(lhs xyz: Int, rhs abc: Int) -> Int { }
>
> So maybe I think about this incorrectly, but I always think of any parameter without an explicit label to have one that is equal to the parameter name.
We’re going in that direction, but that is not currently the case for operators and subscript indices. As a community, we don’t have much real-world experience using Swift 3, but I don’t expect that either of those cases would get better by requiring labels by default and requiring _ to suppress them.
> So these two functions signatures would be equivalent:
>
> func sum1(lhs: Int, rhs: Int) -> Int
> func sum2(lhs lhs: Int, rhs rhs: Int) -> Int
>
> It’s only when you explicit “erase” the label where there is none:
>
> func sum(_ lhs: Int, _ rhs: Int) -> Int
>
> So back to the example above, it’s still somewhat odd that all of these are valid:
>
> hi(1, y: 2, fn: sum1)
> hi(1, y: 2, fn: sum2)
> hi(1, y: 2, fn: sum) // makes the most sense, no label to labeled promotion
>
> But if we did reject the differently labeled version, that would mean that we would need to declare the `Functor` above as:
>
> typealias Functor = (Int, Int) -> Int
I agree that eliminating relabeling would force this.
> Is that better? I’m not terribly convinced that it is.
Me neither.
Off in crazy space for a bit, but one could argue that this style of design would imply that you should be able to call this example like this:
> hi(1, y: 2, fn: { sum1(lhs: $left, rhs: $right) })
since the only sensible names for the parameters come from context.
-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160424/076fbcb2/attachment.html>
More information about the swift-evolution
mailing list