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

devfreak

I have to pass an interface as a parameter to a function. Interface is generic a.k.a. has a associated type. I couldn't find a good way to do that. Here is my code:

protocol IObserver : class {
    typealias DelegateT
    ...
}

class Observer: IObserver {
    typealias DelegateT = IGeneralEventsDelegate // IGeneralEventsDelegate is a protocol
    ...
}

func notify(observer: IObserver) { ... } // here I need a type for observer param

I found that this will work:

func notify<T: IObserver where T.DelegateT == IGeneralEventsDelegate>(observer: T) { ... }

, but come on that is too complicated. What if I want to save this param in class variable, should I make the whole class generic, just because of this function.

It is true that I'm C++ developer and I'm new to the Swift language, but the way the things are done are far too complicated and user unfriendly ... or I'm too stupid :)

Antonio

If you use typealias in a protocol to make it generic-like, then you cannot use it as a variable type until the associated type is resolved. As you have probably experienced, using a protocol with associated type to define a variable (or function parameter) results in a compilation error:

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

That means you cannot use it as a concrete type.

So the only 2 ways I am aware of to use a protocol with associated type as a concrete type are:

  • indirectly, by creating a class that implements it. Probably not what you have planned to do
  • making explicit the associated type like you did in your func

See also related answer https://stackoverflow.com/a/26271483/148357

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 protocol that is using an enum with generic associated type

From Java

swift protocol with associated type

From Dev

Swift Protocol as Generic Parameter

From Dev

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

From Dev

Pass @protocol type in Swift

From Dev

Swift class conforming to protocol, how to pass it as a parameter?

From Dev

Using a Generic Type with a Protocol and associated type?

From Dev

How to define a protocol with an array of a protocol with an associated type

From Dev

Swift supply generic method type parameter when implementing protocol method

From Dev

protocol as parameter type in swift conflicts

From Dev

Swift Self as associated type bound in protocol

From Dev

Swift - Inherited Protocol Associated Type Erasure

From Dev

Swift Type Erasure with Generic Enum and Generic Protocol

From Dev

How do you structure generic type protocol conformance in Swift?

From Dev

How passing a protocol as parameter in Swift

From Dev

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

From Dev

Swift protocol generic as function return type

From Dev

Conforming a generic type to a protocol in a Swift extension

From Dev

Downcast Generic AnyObject to Protocol Associated Type Self.Model

From Dev

Swift protocol with constrained associated type error "Type is not convertible"

From Dev

Swift protocol with constrained associated type error "Type is not convertible"

From Dev

Swift Generic Protocol

From Dev

Protocol function with generic type

From Dev

How create dependent generic protocol in swift

From Dev

How to construct generic without protocol in Swift?

From Dev

How to create a function that returns a generic protocol type?

From Java

How to use generic protocol as a variable type

From Dev

implement protocol with different associated type

From Dev

Protocol functions with Any and Associated Type

Related Related

  1. 1

    Swift protocol that is using an enum with generic associated type

  2. 2

    swift protocol with associated type

  3. 3

    Swift Protocol as Generic Parameter

  4. 4

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

  5. 5

    Pass @protocol type in Swift

  6. 6

    Swift class conforming to protocol, how to pass it as a parameter?

  7. 7

    Using a Generic Type with a Protocol and associated type?

  8. 8

    How to define a protocol with an array of a protocol with an associated type

  9. 9

    Swift supply generic method type parameter when implementing protocol method

  10. 10

    protocol as parameter type in swift conflicts

  11. 11

    Swift Self as associated type bound in protocol

  12. 12

    Swift - Inherited Protocol Associated Type Erasure

  13. 13

    Swift Type Erasure with Generic Enum and Generic Protocol

  14. 14

    How do you structure generic type protocol conformance in Swift?

  15. 15

    How passing a protocol as parameter in Swift

  16. 16

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

  17. 17

    Swift protocol generic as function return type

  18. 18

    Conforming a generic type to a protocol in a Swift extension

  19. 19

    Downcast Generic AnyObject to Protocol Associated Type Self.Model

  20. 20

    Swift protocol with constrained associated type error "Type is not convertible"

  21. 21

    Swift protocol with constrained associated type error "Type is not convertible"

  22. 22

    Swift Generic Protocol

  23. 23

    Protocol function with generic type

  24. 24

    How create dependent generic protocol in swift

  25. 25

    How to construct generic without protocol in Swift?

  26. 26

    How to create a function that returns a generic protocol type?

  27. 27

    How to use generic protocol as a variable type

  28. 28

    implement protocol with different associated type

  29. 29

    Protocol functions with Any and Associated Type

HotTag

Archive