Function that takes a protocol and a conforming class (!) instance as parameters

前端 未结 1 1374
借酒劲吻你
借酒劲吻你 2020-12-17 23:53

I am trying to figure out how to define a function which takes the following two parameters:

  1. A protocol.
  2. An instance of a class (a refere
1条回答
  •  时光说笑
    2020-12-18 00:24

    You can't do what you are trying to do directly. It has nothing to do with reference types, it's because any constraints make T existential so it is impossible to satisfy them at the call site when you're referencing the protocol's metatype P.self: P.Protocol and an adopter C. There is a special case when T is unconstrained that allows it to work in the first place.

    By far the more common case is to constrain T: P and require P: class because just about the only thing you can do with an arbitrary protocol's metatype is convert the name to a string. It happens to be useful in this narrow case but that's it; the signature might as well be register(proto: Any.Type, obj: T) for all the good it will do.

    In theory Swift could support constraining to metatypes, ala register(proto: U, obj: T) but I doubt it would be useful in many scenarios.

    0 讨论(0)
提交回复
热议问题