How to construct generic without protocol in Swift? -


i'm getting following error...

't' cannot constructed because has no accessible initializers

when compiling...

class sub<t : equitable> {     func def(v : t) -> bool{         var d = t()          // <- error         return d == v     } } var s = sub<int>() println(s.def(0), s.def(1))  // i'm expecting "true, false" 

i understand in order generic type initialized, needs conform protocol contains init() constructor. such as...

protocol : equitable {     init() }  class sub<t : a> { 

but get error

type 'int' not conform protocol 'a'

at line

var s = sub<int>()

so how go making value type such int or bool conform protocol can initialized?

you need extend int adopt protocol a:

class sub<t : a> {     func def(v : t) -> bool{         var d = t()           return d == v     } }  protocol : equatable {     init() }  extension int: {  }  var s = sub<int>() s.def(0) // true s.def(1))  // false 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -