Swift root meta-class


(Giacomo Leopizzi) #1

Hello everyone!
I was discussing with a friend about metaclasses in Objective-C. In Obj-C the root meta-class was the NSObject's one. When in a swift class you create a subclass of NSObject, the root metaclass should be the same. What happen when you delcare a class without NSObject dependence? There is an hidden root-class? Where can we read more about this topic?

Thanks,
Giacomo and Annino


(John McCall) #2

Hello everyone!
I was discussing with a friend about metaclasses in Objective-C. In Obj-C the root meta-class was the NSObject's one.

ObjC does not have a single root class. Most ObjC classes inherit from NSObject, but that is not guaranteed, and in fact there are other common root classes including NSProxy.

When in a swift class you create a subclass of NSObject, the root metaclass should be the same. What happen when you delcare a class without NSObject dependence? There is an hidden root-class? Where can we read more about this topic?

When ObjC interop is enabled, Swift classes that do not inherit from NSObject use a private root class. However, that is a private implementation detail and we don't promise much about it.

Do you have any specific questions?

John.

···

On May 8, 2017, at 6:21 PM, Giacomo Leopizzi via swift-dev <swift-dev@swift.org> wrote:


(Giacomo Leopizzi) #3

Our starting point was that in ObjC at the beginning of the execution, the root meta-class (most of the time NSObject) instantiates all the other meta-classes, that create the class as object for the software. In Swift is it the same? If so, which is the name of the root meta-class?
Thanks for the fast reply.

Giacomo

···

On 9 May 2017, at 00:30, John McCall <rjmccall@apple.com> wrote:

On May 8, 2017, at 6:21 PM, Giacomo Leopizzi via swift-dev <swift-dev@swift.org> wrote:
Hello everyone!
I was discussing with a friend about metaclasses in Objective-C. In Obj-C the root meta-class was the NSObject's one.

ObjC does not have a single root class. Most ObjC classes inherit from NSObject, but that is not guaranteed, and in fact there are other common root classes including NSProxy.

When in a swift class you create a subclass of NSObject, the root metaclass should be the same. What happen when you delcare a class without NSObject dependence? There is an hidden root-class? Where can we read more about this topic?

When ObjC interop is enabled, Swift classes that do not inherit from NSObject use a private root class. However, that is a private implementation detail and we don't promise much about it.

Do you have any specific questions?

John.


(John McCall) #4

Our starting point was that in ObjC at the beginning of the execution, the root meta-class (most of the time NSObject) instantiates all the other meta-classes, that create the class as object for the software.

This is somewhat like how a Smalltalk VM is bootstrapped, but there isn't any real sense in which root classes control how classes are created or loaded in ObjC.

In Swift is it the same? If so, which is the name of the root meta-class?

The root class of a Swift class is a private implementation detail; it's not guaranteed in any way.

If you're just curious about the current implementation, you can look in stdlib/public/runtime/SwiftObject.mm, but again, nothing in there is something that you should rely on as a Swift programmer.

John.

···

On May 8, 2017, at 6:46 PM, Giacomo Leopizzi <gi.le@icloud.com> wrote:

Thanks for the fast reply.

Giacomo

On 9 May 2017, at 00:30, John McCall <rjmccall@apple.com> wrote:

On May 8, 2017, at 6:21 PM, Giacomo Leopizzi via swift-dev <swift-dev@swift.org> wrote:
Hello everyone!
I was discussing with a friend about metaclasses in Objective-C. In Obj-C the root meta-class was the NSObject's one.

ObjC does not have a single root class. Most ObjC classes inherit from NSObject, but that is not guaranteed, and in fact there are other common root classes including NSProxy.

When in a swift class you create a subclass of NSObject, the root metaclass should be the same. What happen when you delcare a class without NSObject dependence? There is an hidden root-class? Where can we read more about this topic?

When ObjC interop is enabled, Swift classes that do not inherit from NSObject use a private root class. However, that is a private implementation detail and we don't promise much about it.

Do you have any specific questions?

John.


(Giacomo Leopizzi) #5

Yes, of course. It’s just the will to understand more low level stuffs about Swift. We downloaded the file and what we understand is that the root-class is always SwiftObject but if SWIFT_OBJC_INTEROP is defined it create the necessary “bridges” for working with ObjC. Many thanks for the kind answer, we go to bed because here is almost 2 o’clock am and tomorrow we have to go to the Apple Developer Academy.

Kind regards,
Giacomo and Annino

···

On 9 May 2017, at 01:00, John McCall <rjmccall@apple.com> wrote:

On May 8, 2017, at 6:46 PM, Giacomo Leopizzi <gi.le@icloud.com> wrote:
Our starting point was that in ObjC at the beginning of the execution, the root meta-class (most of the time NSObject) instantiates all the other meta-classes, that create the class as object for the software.

This is somewhat like how a Smalltalk VM is bootstrapped, but there isn't any real sense in which root classes control how classes are created or loaded in ObjC.

In Swift is it the same? If so, which is the name of the root meta-class?

The root class of a Swift class is a private implementation detail; it's not guaranteed in any way.

If you're just curious about the current implementation, you can look in stdlib/public/runtime/SwiftObject.mm, but again, nothing in there is something that you should rely on as a Swift programmer.

John.

Thanks for the fast reply.

Giacomo

On 9 May 2017, at 00:30, John McCall <rjmccall@apple.com> wrote:

On May 8, 2017, at 6:21 PM, Giacomo Leopizzi via swift-dev <swift-dev@swift.org> wrote:
Hello everyone!
I was discussing with a friend about metaclasses in Objective-C. In Obj-C the root meta-class was the NSObject's one.

ObjC does not have a single root class. Most ObjC classes inherit from NSObject, but that is not guaranteed, and in fact there are other common root classes including NSProxy.

When in a swift class you create a subclass of NSObject, the root metaclass should be the same. What happen when you delcare a class without NSObject dependence? There is an hidden root-class? Where can we read more about this topic?

When ObjC interop is enabled, Swift classes that do not inherit from NSObject use a private root class. However, that is a private implementation detail and we don't promise much about it.

Do you have any specific questions?

John.