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

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Jul 15 12:57:50 CDT 2016


Implementation gist updated to reflect everything: https://gist.github.com/DevAndArtist/a5744f21107812b2d4e6baee2c55e0bf

I’ll look into size etc. issue now.

We should tackle this proposal asap because we’re almost out of time for Swift 3.

The public api will look like this:

public final class Type<T> : Hashable, CustomStringConvertible, CustomDebugStringConvertible {
     
    public static var sharedInstance: Type<T> { get }
     
    public static func cast<U>(_ optionalType: Type<U>?) -> Type<T>?

    public func `is`<U>(_ otherType: Type<U>) -> Bool
     
    public var metatype: Metatype<T> { get }
    public static var metatype: Metatype<T> { get }
     
    public var size: Int { get }
    public var stride: Int { get }
    public var alignment: Int { get }
     
    public static var size: Int { get }
    public static var stride: Int { get }
    public static var alignment: Int { get }
     
    public var hashValue: Int { get }
     
    public var description: String { get }
    public var debugDescription: String { get }
}

public func ==<T, U>(lhs: Type<T>, rhs: Type<U>) -> Bool
Additional changes:

public func unsafeBitCast<T, U>(_ x: T, to: Type<U>) -> U

public func dynamicType<T>(_ instance: T) -> Type<T>


-- 
Adrian Zubarev
Sent with Airmail

Am 15. Juli 2016 um 19:26:15, Adrian Zubarev (adrian.zubarev at devandartist.com) schrieb:

Done. Personally I don’t care much if it’s .cast(from:) or .cast(_:).

Now whenever Type<T> is created and if there is no reference in type storage (which I updated to be a Set instead of a Dictionary) that contains the dynamic metatype from our current instance, it automatically downcasts itself to Type<Any> and insert itself into the type storage.

There is one more thing I’d like to discuss before we finally move to size etc.

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

drop init(_ copy: Type<T>) - to keep equality of Type<T> references
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 can’t foresee the future, so I’m wondering if the type may mutate somehow when reflections are added? If so, I don’t see why we should lock references inside a single storage and care about ===.



-- 
Adrian Zubarev
Sent with Airmail

Am 15. Juli 2016 um 19:05:56, Anton Zhilin (antonyzhilin at gmail.com) schrieb:

Brilliant! But I disagree with from label. “Cast from type” means that we somehow use Type<U> statically. But we need to cast optionalType itself to Type<T>. I suggest to use cast(_:) signature.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/3e2e6245/attachment.html>


More information about the swift-evolution mailing list