swift - Protocol extensions on Structs causes compile error 'Self' constrained to non-protocol type -


i'm attempting apply constrained protocol extension struct (swift 2.0) , receiving following compiler error:

type 'self' constrained non-protocol type 'foo'

struct foo: myprotocol {     let myvar: string      init(myvar: string) {         self.myvar = myvar     } }  protocol myprotocol {     func bar() }  extension myprotocol self: foo {     func bar() {         print(myvar)     } }  let foo = foo(myvar: "hello, protocol") foo.bar() 

i can fix error changing struct foo class foo don't understand why works. why can't where self: constrained protocol struct?

this expected behaviour considering struct not meant inherited : notation stands for.

the correct way achieve described equality sign like:

extension myprotocol self == foo {     func bar() {         print(myvar)     } } 

but doesn't compile stupid reason like:

same-type requirement makes generic parameter self non-generic

for it's worth, can achieve same result following:

protocol fooprotocol {   var myvar: string { } } struct foo: fooprotocol, myprotocol {   let myvar: string }  protocol myprotocol {} extension myprotocol self: fooprotocol {   func bar() {     print(myvar)   } } 

where fooprotocol fake protocol foo should extend.

many third-party libraries try extend standard library's struct types (eg. optional) makes use of workaround above.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -