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

Haravikk swift-evolution at haravikk.me
Wed Feb 22 08:22:06 CST 2017


> On 22 Feb 2017, at 13:19, Derrick Ho <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)

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170222/fdefe8ad/attachment.html>


More information about the swift-evolution mailing list