[swift-users] Confusing doc re: metatype type, also doesn't compile on linux

Karl Pickett karl.pickett at gmail.com
Mon Dec 7 19:11:02 CST 2015


This code example:

if someInstance.dynamicType === someInstance.self {
    print("The dynamic and static type of someInstance are the same")
} else {
    print("The dynamic and static type of someInstance are different")
}
// prints "The dynamic and static type of someInstance are different”

Does not compile on Linux.   It also is quite confusing - comparing a type
=== an instance?  It compiles on my mac but is it using some
objc weirdness or simply a bug?

I think the example means to do something like this:

// Note: === won't compile
if SomeBaseClass.self == someInstance.dynamicType {
    print("is base")
} else if SomeSubClass.self == someInstance.dynamicType {
    print("is subclass")
} else {
    print("is neither!")
}

One other minor typo:

“// and the runtime type of someInstance is SomeBaseClass”

should be SomeSubClass.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151207/d90e161d/attachment.html>


More information about the swift-users mailing list