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

Anton Zhilin antonyzhilin at gmail.com
Fri Jul 15 12:58:25 CDT 2016


Consider what I said above, I feel like we should tackle this:

   - drop init(_ copy: Type<T>) - to keep equality of Type<T> references

Agreed.


   - make all initializer internal and as exchange make sharedInstance
   public.

This would ensure that you will always get a reference from the type
storage, and that there can only exist one for each (dynamic) metatype.

I have another idea:

// No uniqueness guarantee
internal class _Type<T> {
    var _metatype: Metatype<T>

    init()
    init(_: AnyMetatype)
    init?<U>(casting: Type<U>)

    // Instead of a global dictionary
    class var sharedInstance: _Type<T>
}
// Uniqueness guarantee
public struct Type<T> {
    internal var impl: _Type<T>

    public init()
    public init(_: AnyMetatype)
    public init?<U>(casting: Type<U>)
}

I can’t foresee the future, so I’m wondering if the type may mutate somehow
when reflections are added?

I don’t think so. If added at all, types should only be able to be created
using some kind of builder:

let builder = TypeBuilder(name: "Person", kind:
.struct)builder.addField(name: "", type: Int.self)
//...let typeRef = builder.create()

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/a36db673/attachment.html>


More information about the swift-evolution mailing list