barndog
barndog

Reputation: 7163

Swift restrict generic type to Type

How can I restrict a generic type to be a type, not an instance of a type?

If I have a class:

class SomeClass<T: SomeProtocol> {}

how can I ensure that T is only an instance of AnyClass (which is just AnyObject.Type)

My protocol only has static methods and in order to call those methods I have to do instance.dynamicType.protocolMethod whereas I want to do someType.protocolMethod

Upvotes: 5

Views: 1653

Answers (2)

user3441734
user3441734

Reputation: 17534

protocol P {
    static func foo()
}
class A : P {
    static func foo() {
        print("A class")
    }
}
class B : P {
    static func foo() {
        print("C class")
    }
}

var type1 = A.self
var type2 = B.self

func check(cls: AnyClass)->Void {
    switch cls {
    case is A.Type:
        A.foo()
    case is B.Type:
        B.foo()
    default:
        break
    }
}

check(type1) // A class
check(type2) // C class

let i = Int.self         // Int.Type
let ao = AnyObject.self  // AnyObject.Protocol
let ac = AnyClass.self   // AnyClass.Protocol
let p = P.self           // P.Protocol
let f = check.self       // (AnyClass)->Void

Edit

protocol P {
    static func f()
}
class A : P {
    static func f() {
        print("A")
    }
}
class B : P {
    static func f() {
        print("B")
    }
}

func f(cls: P.Type) {
    cls.f()
}

f(A)  // A
f(B)  // B

class Test<T: P> {
    func foo() {
        T.f()
    }
}

Test<A>().foo() // A
Test<B>().foo() // B

Upvotes: 0

Paul Cantrell
Paul Cantrell

Reputation: 9314

AFAIK, Swift does not allow you to use a metatype as a generic type. (I believe this is along the lines of what Sam Giddins wished for in Swift 3.)

You can, however, use it as a value. Instead of making T a type parameter, make it a property:

protocol SomeProtocol {
    static func foo()
}

struct Concrete: SomeProtocol {
    static func foo() {
        print("I am concrete")
    }
}

class SomeClass {
    let T: SomeProtocol.Type

    init(T: SomeProtocol.Type) {
        self.T = T
    }

    func go() {
        T.foo()  // no need for dynamicType
    }
}

SomeClass(T: Concrete.self).go()

If, as you say, your protocol contains only static methods, then this is sufficient. However, if you need to tie a generic parameter to the type, that’s possible too:

class SomeClass<U: SomeProtocol> {
    let T: U.Type

    init(T: U.Type) {
        self.T = T
    }

    func go(value: U) {
        T.foo()
    }
}

SomeClass(T: Concrete.self).go(Concrete())

Upvotes: 1

Related Questions