[swift-evolution] [Pitch] Refactor Metatypes

Adrian Zubarev adrian.zubarev at devandartist.com
Sun Oct 2 02:37:34 CDT 2016


That’s right. I was about to say something similar about the Of.

Actually Type<T> would be the correct name for the static/concrete metatype.

We already have this notation but not in a generic way T.Type
If we add of only in our mind to Type<T>, we’ll get “Type of a type T” which is shortly referred as a “metatype of T”. (Array<Element> - Array of Element type)
Since Subtype<T> in reality is some *existential metatype* and the whole talk about existential types now tend to have the Any prefix, lets follow that trend here too.

Type<T> - (static/concrete/exact) metatype.
AnyType<T> - existential metatype.
It might be even easier to memorise this name than Subtype.

The example from the proposal rewritten:

// Types:
protocol Foo {}
protocol Boo : Foo {}
class A : Foo {}
class B : A, Boo {}
struct S : Foo {}

// Metatypes:
let a1: Type<A> = A.self           //=> Okay
let p1: Type<Foo> = Foo.self       //=> Okay
let p2: Type<Boo> = C.self         //=> Error -- `C` is not the same as `Foo`

let any_1: AnyType<Any> = A.self   //=> Okay
let any_2: AnyType<Any> = Foo.self //=> Okay

let a_1: AnyType<A> = A.self       //=> Okay
let p_1: AnyType<Foo> = A.self     //=> Okay
let p_2: AnyType<Foo> = Foo.self   //=> Error -- `Type<Foo>` is not a subtype of `AnyType<Foo>`

// Generic functions:
func dynamic<T>(type: AnyType<Any>, `is` _: Type<T>) -> Bool {
  return type is AnyType<T>
}

func dynamic<T>(type: AnyType<Any>, `as` _: Type<T>) -> AnyType<T>? {
  return type as? AnyType<T>
}

let s1: Type<S> = S.self

dynamic(type: s1, is: Foo.self)    //=> true
dynamic(type: s1, as: Foo.self)    //=> an `Optional<AnyType<Foo>>`
The type(of:) function could now stay, because now we’d only need to change the return type.

func type<T>(of instance: T) -> AnyType<T>
Isn’t this a good compromise?



-- 
Adrian Zubarev
Sent with Airmail

Am 2. Oktober 2016 um 09:01:45, Pyry Jahkola via swift-evolution (swift-evolution at swift.org) schrieb:

OTOH, we don't have ArrayOf<Element> or DictionaryOfKeyAndValue<Key, Value>, while we still pronounce them that way. I don't think prepositions belong in the name of types but in exceptional cases, and this doesn't sound exceptional enough to me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161002/b9c614de/attachment.html>


More information about the swift-evolution mailing list