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

肇鑫 owenzx at gmail.com
Sat Jan 16 06:44:08 CST 2016


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

On Sat, Jan 16, 2016 at 7:04 PM, Haravikk <me at haravikk.com> wrote:

> I think that the point of allowing defaults in protocols is so that you
> can assume a default for all types conforming to that protocol. To use your
> example, if you receive an instance of Name, you can only call
> printSomething() without arguments if you test that it is an instance of
> type Name. If instead you test its conformance to the Good protocol (which
> you might do if there are a lot of different types conforming to Good) then
> you have to provide a value, because you can’t infer that every possible
> implementation will have a default.
>
> Regarding this proposal however I think it might be useful to have a
> distinction between a protocol function that specifies a default value for
> all implementations (that they must all conform to) versus one that
> specifies that implementations must have a default value, but not what that
> value must be.
>
> For example, to have a fixed and altered default we currently we have to
> do things like this:
>
> protocol Protocol {
> func functionWithSpecificDefault(argument:String)
> func functionWithAnyDefault(argument:String)
> }
>
> extension Protocol {
> func functionWithSpecificDefault() {
> self.functionWithSpecificDefault(“Foo”) }
> func functionWithAnyDefault() { self.functionWithAnyDefault(“Foo”) }
> }
>
> class MyClass : Protocol {
> func functionWithSpecificDefault(argument:String) { /* Implementation here
> */ }
> func functionWithAnyDefault(argument:String) { /* Implementation here */ }
>
> func functionWithAnyDefault() { self.functionWithAnyDefault(“Bar”) } //
> Override default
> }
>
> Which could be replaced by:
>
> protocol Protocol {
> func functionWithSpecificDefault(argument:String = “Foo")
> func functionWithAnyDefault(argument:String = default)
> }
>
> class MyClass : Protocol {
> func functionWithSpecificDefault(argument:String = “Foo") { /*
> Implementation here */ }
> func functionWithAnyDefault(argument:String = “Bar") { /* Implementation
> here */ }
> }
>
> However, this has the added advantage that implementing
> functionWithSpecificDefault with a default other than “Foo” would cause a
> compiler error, while doing so for functionWithAnyDefault would not (but
> specifying no default at all would, as one is required).
>
> On 16 Jan 2016, at 10:15, 肇鑫 via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> No. Although you protocol's function doesn't has a default parameter
> value. Your implementation does. So you don't need to define another func
> function() in your protocol.
>
> protocol Good {
>     func printSomething(something:String)
> }
>
> struct Name:Good {
>     func printSomething(something: String = "John") {
>         print(something)
>     }
> }
>
> Name().printSomething()
>
> above code works.
>
> zhaoxin
>
> On Sat, Jan 16, 2016 at 6:05 PM, Vatsal Manot <vatsal.manot at yahoo.com>
> wrote:
>
>> It serves as a better (if not simpler) substitute for the following
>> pattern:
>>
>> protocol Protocol
>> {
>>     typealias Argument
>>
>>     func function()
>>     func function(_: Argument)
>> }
>>
>>
>> On 16-Jan-2016, at 3:29 PM, 肇鑫 <owenzx at gmail.com> wrote:
>>
>> I wonder where is the good for a protocol designer on this?
>>
>> zhaoxin
>>
>> On Sat, Jan 16, 2016 at 5:23 PM, Vatsal Manot via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>>> Currently, the following the code fails with multiple errors:
>>>
>>> protocol Protocol
>>> {
>>>     typealias Argument
>>>
>>>     func function(argument: Argument = default)
>>> }
>>>
>>> I propose that we allow protocols to require functions with default
>>> parameter values. I can’t see any disadvantages to this, and the change
>>> would only be additive.
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>
>>
>>
>>
>> --
>>
>> Owen Zhao
>>
>>
>>
>
>
> --
>
> Owen Zhao
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160116/6e3049e5/attachment.html>


More information about the swift-evolution mailing list