What is ExistentialMetatype in Swift

This surprises me a little. In Swift, try this:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        println(self.superclass) //Outputs "(ExistentialMetatype)"
    }

      

This gives the console output ExistentialMetatype

Going into the Swift NSObject structure, it declares this superclass

as a read-only computable property of typeAnyClass

    var superclass: AnyClass! { get }

      

But nowhere is this word mentioned ExistentialMetatype

. I don't know why, but it resembles the Objective-C runtime (maybe it's the Metatype word in it). Does anyone know about this?

+3


source to share


2 answers


Currently types (instances of metatypes) are printed as useless things like (Metatype)

or (ExistentialMetatype)

. AnyClass

is a AnyObject.Type

metatype. So if you try to print AnyClass

you get this useless description.

However, classes in Objective-C are objects with nice descriptions. Swift allows you to differentiate the types of classes into objects so that they can be used in Objective-C mode. If you type it in first AnyObject

, you will see the class name:



println(self.superclass as AnyObject!)

      

+7


source


If I remember correctly what's going on is that AnyClass is a specially crafted protocol type - a protocol type that each "reference type" (class) automatically matches

The protocol types are sometimes referred to as the "existential types" of your friendly compiler engineers (eg http://www.haskell.org/haskellwiki/Existential_type )



In this case, you are looking at an object of a type that represents an existential type, "metatype" as you say. Hence, "ExistentialMetatype"!

+1


source







All Articles