skip icon indicating copy to clipboard operation
skip copied to clipboard

Support static protocol generic access from reified types

Open marcprux opened this issue 5 months ago • 0 comments

Accessing static members of generic parameters could be supported with Skip's reified type support like so:

protocol Foo {
    static var staticValue: Int { get }
}

struct FooInstance : Foo {
    static var staticValue: Int = 123
}

@inline(__always) func getFooValue<T: Foo>(_ type: T.Type) -> Int {
    // Without this we get: Type parameter 'T' cannot have or inherit a companion object, so it cannot be on the left-hand side of a dot.
    // SKIP REPLACE: return (T::class.companionObjectInstance as FooCompanion).staticValue
    return T.staticValue
}

Skip could automatically identify when there is an attempt to access a static value of a generic reified type T that conforms to X and replace T. with (T::class.companionObjectInstance as XCompanion).

marcprux avatar Jul 21 '25 05:07 marcprux