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

Sune Foldager cyano at me.com
Mon Jan 18 10:53:53 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.

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/20160118/3fb86f83/attachment.html>


More information about the swift-evolution mailing list