Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

Kevin Sylvestre

I have a class that takes a generic class Collection: <T: Model> (Model is a class) and a protocol (Resource) that some of the subclasses of Collection implement:

class Collection: <T: Model> {
  typealias Callback = (result: Collection <T>) -> ()
}
protocol Resource {...}

Is it possible to write a protocol extension where Self is an instance of Collection?

Trying to extend the protocol with the class that takes a generic:

extension Resource where Self: Collection {
  func fetch() {}
}

Gives:

Reference to generic type 'Collection' requires arguments in <...>

Trying to extend the class that takes a generic with the protocol:

extension Collection where Self: Resource {
  func fetch(callback: Callback?) {}
}

Gives:

'Self' is only available in a protocol or as the result of method in a class

I'm not sure how to proceed. The goal is for the function to only be available on instances of Collection that conform to Resource.

Code Different

The problem is Collection is a generic class so every where you declare it, you must attached the specialized type, like Collection<T>. However extension to a protocol can't be specified with a generic type so you end up not being able to supply T to Collection.

In your case though, T is constrained to be of type Model so why not use that in the default protocol implementation:

extension Resource where Self: Collection<Model> {
    func fetch() {
        // default implementation
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

From Dev

Access associated type of a custom protocol in a where clause on generic types in Swift

From Dev

Swift Type Erasure with Generic Enum and Generic Protocol

From Dev

Swift Self as associated type bound in protocol

From Dev

Swift protocol that is using an enum with generic associated type

From Dev

Swift protocol generic as function return type

From Dev

Conforming a generic type to a protocol in a Swift extension

From Dev

System.Collections.Generic.List<T> requires '1' type arguments

From Dev

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

From Dev

How to pass protocol with associated type (generic protocol) as parameter in Swift?

From Dev

Downcast Generic AnyObject to Protocol Associated Type Self.Model

From Dev

Protocol with property of type Self can only be used as generic constraint, why?

From Dev

Protocol with property of type Self can only be used as generic constraint, why?

From Dev

Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

From Dev

Swift Array of Self-defined Objects Contains: Requires to Insert 'where:'

From Dev

Define a Swift protocol which requires a specific type of sequence

From Dev

Error: Protocol requires a nested type '_BitsType' (Swift.FloatingPointType)

From Dev

Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

From Dev

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

From Dev

Objective C syntax corresponding to Swift "extension where self: <some protocol>"

From Dev

Extending Generic Integer Types in Swift

From Dev

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

From Dev

Swift generic class type both subclass and conforms to protocol

From Dev

Swift supply generic method type parameter when implementing protocol method

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

How do you structure generic type protocol conformance in Swift?

From Dev

Extending type aliases in swift

From Dev

Swift Protocol as Generic Parameter

Related Related

  1. 1

    Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

  2. 2

    Access associated type of a custom protocol in a where clause on generic types in Swift

  3. 3

    Swift Type Erasure with Generic Enum and Generic Protocol

  4. 4

    Swift Self as associated type bound in protocol

  5. 5

    Swift protocol that is using an enum with generic associated type

  6. 6

    Swift protocol generic as function return type

  7. 7

    Conforming a generic type to a protocol in a Swift extension

  8. 8

    System.Collections.Generic.List<T> requires '1' type arguments

  9. 9

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

  10. 10

    How to pass protocol with associated type (generic protocol) as parameter in Swift?

  11. 11

    Downcast Generic AnyObject to Protocol Associated Type Self.Model

  12. 12

    Protocol with property of type Self can only be used as generic constraint, why?

  13. 13

    Protocol with property of type Self can only be used as generic constraint, why?

  14. 14

    Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

  15. 15

    Swift Array of Self-defined Objects Contains: Requires to Insert 'where:'

  16. 16

    Define a Swift protocol which requires a specific type of sequence

  17. 17

    Error: Protocol requires a nested type '_BitsType' (Swift.FloatingPointType)

  18. 18

    Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

  19. 19

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

  20. 20

    Objective C syntax corresponding to Swift "extension where self: <some protocol>"

  21. 21

    Extending Generic Integer Types in Swift

  22. 22

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

  23. 23

    Swift generic class type both subclass and conforms to protocol

  24. 24

    Swift supply generic method type parameter when implementing protocol method

  25. 25

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

  26. 26

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

  27. 27

    How do you structure generic type protocol conformance in Swift?

  28. 28

    Extending type aliases in swift

  29. 29

    Swift Protocol as Generic Parameter

HotTag

Archive