Swift generic type that conform to protocol cannot be used to refer protocol?

user1306602
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

Bryan Chen

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift 2.0: Creating a Collection Type of Objects that conform to a Generic Protocol

From Dev

Swift type does not conform to protocol error at generic constraint but not at class itself

From Dev

Swift type does not conform to protocol error at generic constraint but not at class itself

From Dev

Swift: type does not conform to protocol

From Dev

Type does not conform to protocol Swift

From Dev

Why can't a protocol not be used as a type for a generic type in swift?

From Dev

Swift: Type does not conform to protocol 'ArrayLiteralConvertible'

From Dev

Type "myViewController" does not conform to protocol UIPIckerDataSource in Swift

From Dev

Swift 2.0 Type '()' does not conform to protocol

From Dev

Swift type does not conform to protocol NilLiteralConvertible

From Dev

Make property of type and also conform to protocol in Swift

From Dev

Swift - MultipeerConnectivity Type does not conform to protocol

From Dev

Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource'

From Dev

Swift : Type XXX must conform to protocol 'NSObjectProtocol'

From Dev

Swift nested generics type does not conform to protocol

From Dev

Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate'

From Dev

Swift - MultipeerConnectivity Type does not conform to protocol

From Dev

Swift func - Does not conform to protocol "Boolean Type"

From Dev

Swift NSManagedObject does not conform to protocol sequence Type

From Dev

Swift - Type 'CircularTransition' does not conform to protocol 'UIViewControllerAnimatedTransitioning'

From Dev

Conform to protocol in ViewController, in Swift

From Dev

Swift does not conform to protocol

From Dev

Type does not conform to protocol

From Dev

Type () does not conform to protocol

From Dev

Swift Type Erasure with Generic Enum and Generic Protocol

From Dev

Swift Type "SceneName" does not conform to protocol ADInterstitialAdDelegate - SK Swift

From Dev

Type does not conform to protocol - typealias to another protocol

From Java

SwiftUI - Value of protocol type 'Any' cannot conform to 'View'

From Dev

ViewController does not conform to protocol on Swift

Related Related

  1. 1

    Swift 2.0: Creating a Collection Type of Objects that conform to a Generic Protocol

  2. 2

    Swift type does not conform to protocol error at generic constraint but not at class itself

  3. 3

    Swift type does not conform to protocol error at generic constraint but not at class itself

  4. 4

    Swift: type does not conform to protocol

  5. 5

    Type does not conform to protocol Swift

  6. 6

    Why can't a protocol not be used as a type for a generic type in swift?

  7. 7

    Swift: Type does not conform to protocol 'ArrayLiteralConvertible'

  8. 8

    Type "myViewController" does not conform to protocol UIPIckerDataSource in Swift

  9. 9

    Swift 2.0 Type '()' does not conform to protocol

  10. 10

    Swift type does not conform to protocol NilLiteralConvertible

  11. 11

    Make property of type and also conform to protocol in Swift

  12. 12

    Swift - MultipeerConnectivity Type does not conform to protocol

  13. 13

    Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource'

  14. 14

    Swift : Type XXX must conform to protocol 'NSObjectProtocol'

  15. 15

    Swift nested generics type does not conform to protocol

  16. 16

    Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate'

  17. 17

    Swift - MultipeerConnectivity Type does not conform to protocol

  18. 18

    Swift func - Does not conform to protocol "Boolean Type"

  19. 19

    Swift NSManagedObject does not conform to protocol sequence Type

  20. 20

    Swift - Type 'CircularTransition' does not conform to protocol 'UIViewControllerAnimatedTransitioning'

  21. 21

    Conform to protocol in ViewController, in Swift

  22. 22

    Swift does not conform to protocol

  23. 23

    Type does not conform to protocol

  24. 24

    Type () does not conform to protocol

  25. 25

    Swift Type Erasure with Generic Enum and Generic Protocol

  26. 26

    Swift Type "SceneName" does not conform to protocol ADInterstitialAdDelegate - SK Swift

  27. 27

    Type does not conform to protocol - typealias to another protocol

  28. 28

    SwiftUI - Value of protocol type 'Any' cannot conform to 'View'

  29. 29

    ViewController does not conform to protocol on Swift

HotTag

Archive