3

Check the following method definition:

func method<T,K>(protocol: K, object: T) where T:AnyObject, K:Protocol {
    // Do stuff here
}
  • T is an AnyObject
  • K is a Protocol

Now I would like to add an additional constraint: the object of type T must implement the protocol K.

Something like this:

func method<T,K>(protocol: K, object: T) where T:AnyObject, K:Protocol, T:K {
    // Do stuff here
}

Note the T:K added in the where clausule.

When I try this, I get the error:

Type 'T' constrained to a non-protocol, non-class type 'K'

Is this achievable somehow?

vilanovi
  • 2,097
  • 2
  • 21
  • 25
  • 1
    This *might* be a similar problem as in https://stackoverflow.com/questions/37707450/function-that-takes-a-protocol-and-a-conforming-class-instance-as-parameters. – Martin R Apr 11 '18 at 09:35

0 Answers0