[swift-evolution] [Proposal] Allow protocols to require functions with default parameter values.

zhaoxin肇鑫 owenzx at gmail.com
Mon Jan 18 19:34:38 CST 2016


>
> I’m -1 on this feature. Default values is basically compile sugar for not
> filling in the value at the call site. The protocol should clearly state
> its purpose and not constrain its implementations with stuff like this.


I
​ disagree. According to the definition of ​

​Protocol
<https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-method-declaration>
.​

Protocol method declarations have the same form as function declarations,
> with two exceptions: They don’t include a function body, and you can’t
> provide any default parameter values as part of the function declaration.


The definition doesn't say we can't use default values in implementation​.
The definition doesn't say we can't declare that a function must have or
have not default values. Only you can't set a default value in the protocol
function declaration.

This proposal is an improvement to the protocol and violate nothing.

zhaoxin

On Tue, Jan 19, 2016 at 12:53 AM, Sune Foldager <cyano at me.com> wrote:

> I’m -1 on this feature. Default values is basically compile sugar for not
> filling in the value at the call site. The protocol should clearly state
> its purpose and not constrain its implementations with stuff like this.
>
> Also, the reason you’re getting “Good, Good” below is probably because
> default parameters are a bit of a “last resort” in overload resolution.
>
> -Sune
>
> On 16 Jan 2016, at 13:44, 肇鑫 via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Hi Haravikk,
>
> I just implement a func like your examples. However, I find the result is
> interesting.
>
> protocol Good {
>     func printSomething(something:String)
> }
>
> extension Good {
>     func printSomething() { self.printSomething("Good") }
> }
>
> struct Name:Good {
>     func printSomething(something: String = "John") {
>         print(something)
>     }
> }
>
> struct Talk:Good {
>     func printSomething(something: String = "Hahaha") {
>         print(something)
>     }
> }
>
> var protocols:[Good] = [Name(), Talk()]
>
> for p in protocols {
>     p.printSomething() // Good, Good instead of John, Hahaha
> }
>
> As you can see, the final result is "Good, Good", not as I expected "John,
> Hahaha", is that right or is it a bug?
>
> zhaoxin
>
>
> […snip…]
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160119/ad615e61/attachment.html>


More information about the swift-evolution mailing list