how to declare a generate protocol as delegate?

we can make generate protocol like the follow:



protocol SomeDelegate {
typealias T
func xxx(x: T)
}


and make some class conform it:



class AA: SomeDelegate {
typealias T = Int
func xxx(x: T) {
// do some thing
}
}


and my problem was how to declare some propert conform to the generate protocol like this:



class BB {
var delegate: SomeDelegate
}


the code on above will raise error:



Protocol 'SomeDelegate' can only be used as a generic constraint 
because it has Self or associated type requirements


It Seems I can use the protocol as delegate like follow:



class BB {
var delegate: AA?
}


but, this was not I want, it will cause my delegate can't inherit other parent class



Answers

You could use generics, using SomeDelegate as a type constraint:



class BB <U : SomeDelegate> {
var delegate: U? = nil
}


This way you'll only need to supply the type of U when you initialise an instance of BB:



struct MyStruct : SomeDelegate {
// The argument of xxx needs to be a concrete type here.
func xxx(x: Int) {
// ...
}
}

let bb = BB<MyStruct>()


Answers

There are few ways you can get through this error.



protocol SomeDelegate {
typealias T
func xxx(x: T)
}


Firstly use the proper type class for the function and the compiler infers the type itself.



 class AA: SomeDelegate {
func xxx(x: Int) {
println(x)
}
}


Or, you can also override the typealias and assign it the proper type class like,



class AA: SomeDelegate {
typealias T = Int
func xxx(x: T) {
println(x)
}
}


And then use it just like the way you have done,



class B {
var someDelegate: AA?
}