JMI
JMI

Reputation: 2628

Is it possible to create class only extension for protocol?

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.

Upvotes: 1

Views: 308

Answers (1)

Martin R
Martin R

Reputation: 539685

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.

Upvotes: 4

Related Questions