[swift-evolution] Allow strengthening argument types in functions declared for protocol conformance

Terrence Katzenbaer tkatzenbaer at me.com
Wed Dec 16 13:43:59 CST 2015


Because APIs are designed to be generic, protocols that must be conformed generally use types like Object or other base classes for a given framework. This introduces type casting verbosity when implementing the protocol for a specific use. I would like to see the ability to strengthen argument types in functions declared for protocol conformance.

An example:
class Foo { }

class Bar: Foo { }

protocol FooDelegate {
    func didPerformSomeAction(object: Foo)
}

class FooController: FooDelegate {
    func didPerformSomeAction(var object: Foo) {
        // I know that object must be a Bar instance
        object = object as! Bar
        // do something with object
    }
}

class ProposedFooController: FooDelegate { // Type 'ProposedFooController' does not conform to protocol 'FooDelegate'
    func didPerformSomeAction(object: Bar) {
        // do something with Bar instance
    }
}

The glaring issue I see outright is how the runtime should fail when `didPerformSomeAction` in `ProposedFooController` is called with a non-Bar instance... But perhaps it /should/ fail outright because the programmer has explicitly stated that the type should be Bar.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/e3c1718d/attachment.html>


More information about the swift-evolution mailing list