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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.