[swift-evolution] When to use argument labels (a new approach)
Douglas Gregor
dgregor at apple.com
Wed Feb 3 11:44:13 CST 2016
> On Feb 2, 2016, at 10:17 PM, Jonathan Tang via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>
> On Tue, Feb 2, 2016 at 4:32 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>
> 2. Words that describe attributes of an *already-existing* instance
> should go in the base name rather than in a label:
>
> a.tracksHavingMediaType("Wax Cylinder") // yes
> a.removeFirstTrackHavingMediaType("BetaMax") // yes
>
> a.tracks(mediaType: "Wax Cylinder") // no
> a.removeFirstTrack(havingMediaType: "BetaMax") // no
>
> [yes, we could use "With" instead of "Having", but it's more
> ambiguous]
>
> Words that describe attributes of an instance *to be created* should
> go in argument labels, rather than the base name (for parity with
> initializers):
>
> AudioTrack(mediaType: "BetaMax") // initializer
> trackFactory.newTrack(mediaType: "Wax Cylinder") // yes
>
> trackFactory.newTrackWithMediaType("Wax Cylinder") // no
>
>
> Very mixed feelings on this, probably adding up to a +0.2 or so. I'll mention a couple concerns that I haven't seen anyone raise:
>
> Please consider the first-class function case when naming. Particularly since Swift encourages passing functions around as objects rather than using string selectors. #2 implies that the prepositional phrase will appear when *referencing* the method (vs. calling it):
>
> let ops = [
> self.removeFirstTrackHavingMediaType,
> self.addTrackWithMediaType
> self.populateTrackOperationsForMediaType
> self.play
> ]
>
> vs.
>
> let ops = [
> self.removeFirstTrack
> self.addTrack
> self.populateTrackOperations
> self.play
> ]
>
> The second option wins on verbosity, but the first arguably gives more clarity as to what the methods actually do. Also, the second has a potentially annoying semantic problem: if you have overloads for these methods that differ only in keyword, Swift won't be able to disambiguate them:
>
> // Compile error: Invalid redeclaration of removeFirstTrack
> func removeFirstTrack(havingMediaType: String) { ... }
> func removeFirstTrack(named: String) { ... }
> func removeFirstTrack(byArtist: String) { ... }
>
> // Compile error only when the function is referenced
> func removeTrack(n: Int, named: String)
> func removeTrack(n: Int, byArtist: String)
> let f = self.removeTrack // named: or byArtist:?
>
> // Legal...
> func removeFirstTrackHavingMediaType(_: String) { ... }
> func removeFirstTrackNamed(_: String) { ... }
> func removeFirstTrackByArtist(_: String) { ... }
>
> Unless the Swift compiler were to change to make the former set legal, I think this is a powerful argument in favor of this proposal, because otherwise you may find that the compiler prevents you from writing the code that the guidelines encourage. You might also find that changing the type of an overload means you have to change the name to prevent a collision, which could be very surprising to users.
I suspect that you missed SE-0021:
https://github.com/apple/swift-evolution/blob/master/proposals/0021-generalized-naming.md
which lets you name the arguments in a function reference, e.g:
let f = self.removeTrack(_:named:)
let f2 = self.removeTrack(_:byArtist:)
let ops = [
self.removeFirstTrack(mediaType:),
self.addTrack(mediaType:),
self.populateTrackOperationsForMediaType,
self.play
]
- Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160203/630627e6/attachment-0001.html>
More information about the swift-evolution
mailing list