[swift-evolution] [Pitch] Rename `AnyObject` to `AnyClass` and drop current `AnyClass`

Adrian Zubarev adrian.zubarev at devandartist.com
Thu Jun 9 05:08:08 CDT 2016


I added a draft proposal here: https://github.com/DevAndArtist/swift-evolution/blob/rename_anyobject_remove_anyclass/proposals/nnnn-rename-anyobject-remove-anyclass.md

Rename AnyObject and remove current AnyClass

Proposal: SE-NNNN
Author(s): Adrian Zubarev
Status: Awaiting review
Review manager: TBD
Introduction

From the beginning AnyObject protocol and AnyClass type-alias felt wrong and confusing. This proposal aims to sort out the confusion and provide a consistency for future version of Swift.

Swift-evolution thread: [Pitch] Rename AnyObject to AnyClass and drop current AnyClass

Motivation

In Swift 3 the standard library will correspond to a particular guideline. This means that the Type suffix will be removed almost everywhere. This provides good readability and makes usage of .Type more clearer and consistent. Furthermore this change is a first step towards consistency for generalized existentials.

Short example:

-func construct(type: AnyClass) -> AnyObject?
+func construct(type: AnyClass.Type) -> AnyClass? {
    if let objectType = type as? NSObject.Type {
        return objectType.init()
    } else {
        return nil
    }
}
Proposed solution

Remove current AnyClass type-alias from the standard library and rename current AnyObject protocol to AnyClass. Then migrate existing code from AnyClass to AnyClass.Type and from AnyObject to AnyClass.

// Remove completely
-public typealias AnyClass = AnyObject.Type

// Rename to AnyClass
- at objc public protocol AnyObject {}
+ at objc public protocol AnyClass {}
Impact on existing code

This change will break existing code, and will require a migrator to translate Swift 2 code into Swift 3 code.

Alternatives considered & Future consistency

Use generalized existentials with any-class requirement Any<class> instead, which won’t make it into Swift 3 in time:
AnyClass equals Any<class> or typealias AnyClass = Any<class>
AnyClass.Type equals Any<class>.Type
Add AnyValue type-alias with generalized existentials and value keyword:
AnyValue equals Any<value> or typealias AnyValue = Any<value>
AnyValue.Type equals Any<value>.Type
Or instead AnyValue add AnyStruct and AnyEnum type-aliases:
AnyStruct equals Any<struct> or typealias AnyStruct = Any<struct>
AnyEnum equals Any<enum> or typealias AnyEnum = Any<enum>
AnyStruct.Type equals Any<struct>.Type
AnyEnum.Type equals Any<enum>.Type
Example:

// Accept any type that conforms to `SomeProtocol`
func doSomething(with interface: SomeProtocol) { ... }

// Accept any class that conforms to `SomeProtocol`
// We use shorthand syntax for existentials here (SE-0095)
func doSomething(with interfaceReference: AnyClass & SomeProtocol) { ... }
func doSomething(with interfaceReference: Any<class> & SomeProtocol) { ... }

// Accept any value that conforms to `SomeProtocol`
// Missing counterpart to `AnyClass`
func doSomething(with interfaceValue: AnyValue & SomeProtocol) { ... }
func doSomething(with interfaceValue: Any<value> & SomeProtocol) { ... }

// Or more specific value types:
func doSomething(with interfaceValue: AnyStruct & SomeProtocol) { ... }
func doSomething(with interfaceValue: Any<struct> & SomeProtocol) { ... }

func doSomething(with interfaceValue: AnyEnum & SomeProtocol) { ... }
func doSomething(with interfaceValue: Any<enum> & SomeProtocol) { ... }


-- 
Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160609/d7cc0e49/attachment.html>


More information about the swift-evolution mailing list