generics - Using a template type in a swift protocol definition -


how can declare protocol in swift function argument types depend on type adopts protocol?

something

 protocol endianconvertible<t> {        init( litteendian: t );        func litteendian() -> t;  } 

background: i'm trying write stream decoder little endian binary stream in swift.

part of generic function

class readstream {   var offset : int = 0;  var data : data;   func readintle<t : integer>() -> t {     var d : t = 0     let intbits = data.withunsafebytes({(bytepointer: unsafepointer<uint8>) -> t in         bytepointer.advanced(by: offset).withmemoryrebound(to: t.self, capacity: memorylayout<t>.size) { pointer in             offset += memorylayout<t>.size             return pointer.pointee         }     })     d = t(littleendian: intbits)   } }  

which fails compile integer protocol not specify init(litteendian: t) available, , obvious way fix seemed have protocol adds specification.

you can use self in protocol reference concrete type of class/type implements it:

protocol endianconvertible {        init( litteendian: self );        func litteendian() -> self; } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -