1

Class only protocol usage is clear to me. I know I can do something like:

protocol HashableClass: class, Hashable {}

But I wonder if it is possible to create class only extension:

extension Hashable: class {} / extension Hashable where Self is class {} ??

The second question is: Is it possible to create non class protocols (counter part of class only protocols)?

Thank you in advance.

JMI
  • 2,530
  • 15
  • 26

1 Answers1

4

All classes implicitly conform to AnyObject, so you can define a "class-only extension" with

extension Hashable where Self: AnyObject {

}

(Also there seems to be no difference between protocol Foo: class and protocol Foo: AnyObject, compare What's the difference between a protocol extended from AnyObject and a class-only protocol?.)

Restricting a protocol to non-class types is – as far as I know – not possible.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382