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

Anton Zhilin antonyzhilin at gmail.com
Thu Jul 14 19:24:58 CDT 2016

2016-07-15 0:02 GMT+03:00 Adrian Zubarev via swift-evolution <
swift-evolution at swift.org>:

> I still can’t get my head around this.
> Originally I wanted to seal T.Type completely but I realized that there
> are to many downsides, therefore I think it’s better to revise the way how
> we construct a metatype in Swift + how to solve the ambiguity in
> array/dictionary shorthand syntax + open the door for reflections +
> combining SE–0101 into Type<T>.
> I don’t think we have to make the whole model of Type<T> that complicated
> that it contains different identifier for each type. This is already solved
> with metatypes.
We can have a field of type T.Type, that is what I meant, no need in
My point is that if we want to remove T.Type from the language, then we
can't just refer to it in docs.
We can use whatever we want for implementation, though.

The thing I believe you misinterpret with Type<T> being the same as T.Type is
> that if Type<T> would act like T.Type:
> T.self.init == T.init
> T.self.staticMember == T.staticMember
> We won’t be able to extend Type<T> at all because otherwise all static
> members of Type<T> would be reserved and cannot be implemented in T,
> which is clearly not how it should work.
> Type<T>.init != T.init
> Type<T>.staticMember != T.staticMember
> Thats why I’m talking about the metatype property all the time.

Ok, I forgot about it. Type<T> should have all features of T.Type, *except
that* Type<T> will not contain static methods of T.
I still can't see any concrete examples where this behaviour is necessary.
Metatypes T.Type can become an implementation detail of Type<T>.

    // do not construct full `Type<T>` - use lightweight static calculation
> instead
>     public var size: Int { return Type<T>.size }
>     public var stride: Int { return Type<T>.stride }
>     public var alignment: Int { return Type<T>.alignment }
>     public static var size: Int { return _sizeof(Type<T>.metatype) }
>     public static var stride: Int { return _strideof(Type<T>.metatype) }
>     public static var alignment: Int { return _alignof(Type<T>.metatype) }

I agree with most of the code (without _uniqueIdentifier) but not with
this. Consider this example:

let x = Type<Int>()
let y = Type<CustomStringConvertible>(casting: x)!
x.size  //=> 8
y.size  //=> 8 or 40?

I think that y.size should be 8, because we are dynamically checking size
of particular type y, specific subtype of CustomStringConvertible. We
should look into corresponding witness table or whatever, and get the size
at runtime.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/c4522fe7/attachment.html>

More information about the swift-evolution mailing list