[swift-evolution] [Discussion] Seal `T.Type` into `Type<T>`

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Jul 15 11:18:58 CDT 2016


Think of metatypes as standalone types, which they actually are.

To get a metatype in Swift 2.2 we use this TypeName.self which return an instance of TypeName.Type.

Crystal clear right?

Int.self -> Int.Type
UIView.self -> UIView.Type
But there is one type which is bugged: Any

Any.self -(is really)-> Any.Type
Any.Type in declarations is not equal the return type from Any.self
// Swift 2.2
func test<T>(_ metatype: T.Type) { print(metatype) }

class A {}

let a = A()
let hidden: Any = A()

let dynamicAMetatype = a.dynamicType

// produces `Any.Type` which we expect
let dynamicAnyMetatype = hidden.dynamicType

test(dynamicAMetatype) // prints "A"

// BUT!!!!!
// ERROR: Cannot invoke 'test' with an argument list of type '(Any.Type)'
test(dynamicAnyMetatype)
This is a problem when you try to downcast your metatype to the metatype for Any.

A workaround is to use Any.Type which means is can only store any metatype (like: Base.self, Derived.self, Any.self etc.)

This is clearly a bug to me. SR–2085



-- 
Adrian Zubarev
Sent with Airmail

Am 15. Juli 2016 um 17:12:29, Anton Zhilin (antonyzhilin at gmail.com) schrieb:

I just realized what causes the SR–2085 bug I mentioned earlier.
Any.Type is not the metatype of Any
Any.Type means that this ‘thing’ can store any metatype
There is no type that expresses metatype for Any
We still can get the metatype for Any through Any.self
I updated SR–2085 and suggested that we need AnyMetatype.

Why? I would prefer to be consistent and make Metatype<Any> the metatype of Any (excuse the pun). And because all types are subtypes of Any, instances of Metatype<Any> will still cover all types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/4db35707/attachment.html>


More information about the swift-evolution mailing list