Class-only protocols, class vs AnyObject?

What is the difference, if any, btw P and Q here:

protocol P : class { ... }

protocol Q : AnyObject { ... }

If no difference, which should be preferred?

(I got the impression that the class version was meant to be deprecated, but it still compiles without warnings as of Xcode 9.3)

3 Likes

See swift - What's the difference between a protocol extended from AnyObject and a class-only protocol? - Stack Overflow for a subtle difference, but yes it has been stated that it will be removed. I also don't know why it wasn't deprecated in this release.

1 Like

Thanks, If the following (from @codafi's stack overflow answer) is true:

The difference lies in the semantics of @objc declarations. With AnyObject, the expectation is that conforming classes may or may not be proper Objective-C objects, but the language treats them as such anyway (in that you lose static dispatch sometimes).

Does this mean that inheriting from AnyObject might incur a performance overhead (compared to if class had been used)?

The StackOverflow answer is out of date as of Swift 4. protocol P : class {} and protocol P : AnyObject {} parse identically and there's no semantic difference between them. We should add a warning to deprecate the former at some point.

23 Likes

The StackOverflow answer is out of date as of Swift 4. protocol P : class {} and protocol P : AnyObject {} parse identically and there’s no semantic difference between them. We should add a warning to deprecate the former at some point.

But even @jrose prefers class in his WWDC 2018 talk :stuck_out_tongue_winking_eye:

1 Like

It's just fair that Swift Evolution not only breaks users code, but also creators slides ;-)

8 Likes

Oof, my bad. Should have had Slava review them first!

10 Likes

Don't worry, you weren't the only presenter with imperfect Swift code. I'm pretty sure I remember a session where the presenter typed out -> Void return types. It's fairly clear that many Apple's still aren't that familiar with Swift. For some, I'm guessing WWDC slides and presentations may be the only time they write Swift at all. At least I haven't noticed any semicolons this year!

5 Likes

Which keyword is preferred? Assumed that seems 'class' behaves incorrectly, is AnyObject better? Will class be deprecated?

Both are the same, class is deprecated in favor of AnyObject simply without a warning. See Slavas response above.

1 Like

Both keywords behave identically. AnyObject is preferred because it is the name of a type and can appear anywhere that a type can. "class" is just special syntax in that one place in a protocol's inheritance clause, and it parses identically to "AnyObject".

11 Likes

Quoting Matt Neuburg:

But... the 0156 proposal says:

This proposal merges the concepts of class and AnyObject , which now have the same meaning: they represent an existential for classes. To get rid of the duplication, we suggest only keeping AnyObject around. To reduce source-breakage to a minimum, class could be redefined as typealias class = AnyObject and give a deprecation warning on class for the first version of Swift this proposal is implemented in. Later, class could be removed in a subsequent version of Swift.

0156 proposal

2 Likes

Is this information still valid? Should we avoid using class and use AnyObject instead?

Yes

2 Likes

You can use either one, but I suggest using AnyObject since it can be used in more places (such as protocol compositions, eg MyProto & AnyObject). Also I’d like us to add a warning that class is deprecated at some point (this would make for a great starter bug if anyone is interested).

6 Likes

As of today, class is now officially deprecated in favor of AnyObject.