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
Post a Comment