In another thread, I saw this construction:
protocol AssetInstance : class {
var identifier: String { get set }
}
Which, to my astonishment, compiles. I can't seem to find documentation for this use of the keyword "class".
Could I get a pointer, please?
krilnon
(Kyle Murray)
January 15, 2021, 6:22pm
2
It's another name for AnyObject
.
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.
# Class and Subtype existentials
* Proposal: [SE-0156](0156-subclass-existentials.md)
* Authors: [David Hart](http://github.com/hartbit), [Austin Zheng](http://github.com/austinzheng)
* Review Manager: [Doug Gregor](https://github.com/DougGregor)
* Status: **Implemented (Swift 4)**
* Decision Notes: [Rationale](https://forums.swift.org/t/accepted-se-0156-class-and-subtype-existentials/5477)
* Bug: [SR-4296](https://bugs.swift.org/browse/SR-4296)
## Introduction
This proposal brings more expressive power to the type system by allowing Swift to represent existentials of classes and subtypes which conform to protocols.
[Mailing list discussion](https://forums.swift.org/t/subclass-existentials/5024)
## Motivation
Currently, the only existentials which can be represented in Swift are conformances to a set of protocols, using the `&` protocol composition syntax:
```swift
This file has been truncated. show original
2 Likes
Tino
(Tino)
January 15, 2021, 10:46pm
4
You are lucky —I tell people all the time to use AnyObject
; I guess the old spelling will never fade away
1 Like
phoneyDev
(Stern)
January 15, 2021, 11:20pm
5
It's been deprecated, or something.
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)
Nevin
January 16, 2021, 12:17am
6
Personally, I think class
is much clearer in its meaning than AnyObject
.
1 Like
Hacksaw
January 16, 2021, 12:32am
7
Not for me. Not in lowercase. It looks like a mistake. If is was Class, mirroring Self, maybe.
AnyObject is fine for me.
2 Likes