[swift-evolution] Protocol extension function overrides the default value in implementations

肇鑫 owenzx at gmail.com
Sat Jan 16 06:51:38 CST 2016


I got this by discussions in another thread of swift-evolution.  The result
is interesting. I don't know if it should be treat as a bug.

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


More information about the swift-evolution mailing list