swift2 - Swift 2.0: Protocol extensions: Two protocols with the same function signature compile error -
given 2 protocols , extensions:
protocol firstdelegate { func somefunc() } protocol seconddelegate { func somefunc() } extension firstdelegate { func somefunc() { print("first delegate") } } extension seconddelegate { func somefunc() { print("second delegate") } }
and trying conform both of them:
class someclass: firstdelegate, seconddelegate {}
i receive compile-time error:
type 'someclass' not conform protocol 'firstdelegate'
exchanging firstdelegate
, seconddelegate
:
class someclass: seconddelegate, firstdelegate {}
produces reverse:
type 'someclass' not conform protocol 'seconddelegate'
removing 1 of extensions resolves problem. ditto providing implementation somefunc()
inside someclass
.
this protocol extension functionality rather new me. information in apple's official 'swift programming guide (prerelease)' scarce @ moment.
did violate rules of protocol extensions here?
a protocol defines requirements (methods, properties, ...) conformant type.
protocol firstdelegate { func somefunc() } protocol seconddelegate { func somefunc() }
defines 2 protocols same required method somefunc()
. conformant type must implement method:
class someclass: firstdelegate, seconddelegate { func somefunc() { print("someclass implementation") } }
a protocol extension provides method , property implementations conformant types. special case of protocol extension default implementation, defined here:
extension firstdelegate { func somefunc() { print("first delegate") } }
it defines default implementation of somefunc()
types conforming firstdelegate
. since only required method of protocol, conforming class need not define method @ all:
class someclass: firstdelegate { } someclass().somefunc() // output: first delegate
but if class provides own implementation used:
class someclass: firstdelegate { func somefunc() { print("someclass implementation") } } someclass().somefunc() // output: someclass implementation
in case, have defined default implementations of somefunc()
both protocols:
extension firstdelegate { func somefunc() { print("first delegate") } } extension seconddelegate { func somefunc() { print("second delegate") } }
a class can still conform both protocols if provides own implementation of required method:
class someclass: firstdelegate, seconddelegate { func somefunc() { print("someclass implementation") } }
but class cannot conform using default implementation
class someclass: firstdelegate, seconddelegate { }
for both protocols because there conflict. unspecified default implementation should used, , that's why compiler complains.
actually class conforms none of protocols. can seen in full compiler log in report navigator:
main.swift:24:7: error: type 'someclass' not conform protocol 'firstdelegate' class someclass: firstdelegate, seconddelegate { ^ main.swift:5:10: note: multiple matching functions named 'somefunc()' type '() -> ()' func somefunc() ^ main.swift:19:10: note: candidate matches func somefunc() { ^ main.swift:13:10: note: candidate matches func somefunc() { ^ main.swift:24:7: error: type 'someclass' not conform protocol 'seconddelegate' class someclass: firstdelegate, seconddelegate { ^ main.swift:9:10: note: multiple matching functions named 'somefunc()' type '() -> ()' func somefunc() ^ main.swift:19:10: note: candidate matches func somefunc() { ^ main.swift:13:10: note: candidate matches func somefunc() { ^
Comments
Post a Comment