import UIKit
protocol Identifiable
{
}
protocol Storage
{
func test() -> Data<Identifiable>
}
class DiskStorage<T where T:Identifiable, T:NSCoding>:Storage
{
func test() -> Data<Identifiable>
{
return Data<T>() //error: T is not identical to Identifiable
}
}
class Data<T where T:Identifiable>
{
}
I thought it would be possible to use generic type that conform protocol in order to call method that reference that same protocol. How to cast it? Tried almost everything, nothing is working. Maybe I understand something wrong...
Any help on this one guys? Thanks a lot
try this
protocol Identifiable
{}
class Data<T where T:Identifiable>
{}
protocol Storage
{
typealias Element : Identifiable
func test() -> Data<Element>
}
class DiskStorage<T where T:Identifiable, T:NSCoding>:Storage
{
func test() -> Data<T>
{
return Data<T>()
}
}
// from REPL
32> var s = DiskStorage<Foo>()
s: DiskStorage<Foo> = {}
33> s.test()
$R0: Data<Foo> = {}
As I pointed out in this answer, Data<T>
have no relationship to Data<Identifiable>
. So you can't use Data<T>
in place that expecting Data<Identifiable>
and hence the compile error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With