[swift-evolution] [Pitch] Refactor Metatypes

Adrian Zubarev adrian.zubarev at devandartist.com
Thu Oct 13 02:30:44 CDT 2016

Is there a need of having this function at all? You always know the current static type locally. Sure one might need this in some generic function to extract the static type from an instance, but most of the time we could just write T.self to do that.

If there really is a need for that function, we could move it to future talk when might want to add the following function to the language.

/// This function could find dynamically metatypes if they exist (even value types).
func type/subtype<T>(of type: Type<T>, named name: String) -> AnyType<T>? { … }

protocol P { init() }
struct S : P { init() {} }

let struct: P = subtype(of: P.self, named: "S")!.init() // Neat
struct is S //=> true

// One day when `.self` magic is removed, we'd have this nice form.

let struct: P = subtype(of: P, named: "S")!.init() // Neat

// With the proposed metatype model we could even get the static metatypes for protocols

let protocolMetatype: AnyType<Any> = subtype(of: Any.self, named: "P")!
protocolMetatype is Type<P>    //=> true
protocolMetatype is AnyType<P> //=> false
Again this is something for the future, but I felt like I had to mention it.

I’ll fix the typo, thanks.

Adrian Zubarev
Sent with Airmail

Am 13. Oktober 2016 um 07:09:04, Russ Bishop (xenadu at gmail.com) schrieb:

Are we not going to have func staticType<T>(of instance: T) -> Type<T> ?

(Also you mis-spelled “instace”)

Overall I like it and the whole scheme brings some nice clarity.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161013/5045751a/attachment.html>

More information about the swift-evolution mailing list