[swift-evolution] [Pitch] Allow trailing argument labels

Vladimir.S svabox at gmail.com
Wed Feb 22 09:38:43 CST 2017


On 22.02.2017 17:22, Haravikk via swift-evolution wrote:
>
>> On 22 Feb 2017, at 13:19, Derrick Ho <wh1pch81n at gmail.com
>> <mailto:wh1pch81n at gmail.com>> wrote:
>>
>> I've read the pitch, but it isn't clear what the ask is or what benefit
>> it would give.
>
> The purpose of the feature is to enable selection of identically named
> methods with similar signatures. This came up in the protocol-oriented
> integers debate on the ability to have arithmetic methods with and without
> overflow. Like so:
>
> protocol IntegerArithmetic {
> func adding(_ other:Self) -> Self
> func adding(_ other:Self, reportingOverflow) -> (Self, Bool)
> }
>
> var a = 2, b = 3
> let resultWithoutOverflow = a.adding(b)
> let resultWithOverflow = a.adding(b, reportingOverflow)

Isn't this very confusing about what 'reportingOverflow' *is*? IMO it looks 
like reportingOverflow is a variable/property defined in some place and has 
some value. While "a.adding(b, .reportingOverflow)" is obvious about what 
.reportingOverflow is, just like separate addingWithReportingOverflow method.

Do I understand correctly, that the proposed solution is some kind of sugar 
to not have separate method named like 'addingWithReportingOverflow' ?

In any case IMO such argument label should have special syntax on caller 
site.. don't know, probably:

let resultWithOverflow = a.adding(b, :reportingOverflow)

probably with the same declaration:
func adding(_ other:Self, :reportingOverflow) -> (Self, Bool)


if we keep semicolon *after* such trailing label, then we have the problem :
let x = foo(reportingOverflow:)

was foo called with trailing label 'reportingOverflow' or x is assigned to 
function foo with one 'real' argument?

>
> Here we have two different methods, one reporting overflow and one without,
> using the label to enable selection, rather than having to have an
> addingWithOverflow() method or similar.
>
> Currently the alternative is to do something like:
>
> enum ReportingOverflow { case .reportingOverflow }
> protocol IntegerArithmetic {
> func adding(_ other:Self) -> Self
> func adding(_ other:Self, _) -> (Self, Bool)
> }
>
> var a = 2, b = 3
> let resultWithoutOverflow = a.adding(b)
> let resultWithOverflow = a.adding(b, .reportingOverflow)
>
> Basically the ability to use the label for method selection is an
> alternative to either defining an enum as above, or having to use a
> different method name.
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list