Swift泛型函数受类generic约束

时间:2020-04-21 07:36:47

标签: swift generics compiler-errors generic-constraints

考虑以下代码:

public class Test<P: AnyObject> {
    public func foo<T: P>(_ t: T.Type) -> T { // ERROR: Type 'T' constrained to non-protocol, non-class type 'P'
        // stuff happens
    }
}

请注意第二行的错误,声称P不是类类型。但是,在第1行,P被声明为扩展AnyObject,因此必须为类类型。因此,该错误是不正确的。 ...对?这段代码和/或编译器怎么了?


编辑:这是给出五个相似通用功能的示例。它们都是根据我想要的功能来衡量的,大写字母中的所有注释都指出了它们无法满足我的需求的方式。为了使示例更加具体,我将AnyObject替换为具体的类C0。请注意,C2子类C1的子类C0CX是无关的。

public class C0 {
    public required init() {
    }
}

public class C1: C0 {
    public required init() {
    }
}

public class C2: C1 {
    public required init() {
    }
}

public class CX {
    public required init() {
    }
}

//public class D0<P: AnyObject> { // AnyObject replaced with C0 for a more concrete example
public class D0<P: C0> {
    public func foo0<T: P>(_ t: T.Type) -> T { // DOESN'T COMPILE
        return t.init()
    }
    public static func test_foo0() {
//      let c2_0: C2 = D0<C1>().foo0(C2.self) // Function sig doesn't compile; can't test
//      let cX_0: CX = D0<C1>().foo0(CX.self) // Function sig doesn't compile; can't test
    }



    // Shadows P in favor of its own local generic parameter P (T would accomplish the same result)
    public func foo1<P>(_ t: P.Type) -> P { // TOO PERMISSIVE
        return t.init() // Should compile ; does NOT compile
    }
    public static func test_foo1() {
        let c2_1: C2 = D0<C1>().foo1(C2.self) // Should compile ; does compile
        let cX_1: CX = D0<C1>().foo1(CX.self) // Should not compile ; DOES compile
    }



    public func foo2(_ t: P.Type) -> P { // TOO RESTRICTIVE
        return t.init()
    }
    public static func test_foo2() {
        let c2_2: C2 = D0<C1>().foo2(C2.self) // Should compile ; does NOT compile
        let cX_2: CX = D0<C1>().foo2(CX.self) // Should not compile ; does not compile
    }



    // Hardcoded to match the constraint that is *on P*
    public func foo3<T: C0>(_ t: T.Type) -> T { // TOO PERMISSIVE
        return t.init()
    }
    public static func test_foo3() {
        let c0_3: C0 = D0<C1>().foo3(C0.self) // Should not compile ; DOES compile
    }



    // Hardcoded to match the actual generic parameter of my example
    public func foo4<T: C1>(_ t: T.Type) -> T { // HARDCODED TO MATCH MY SINGLE EXAMPLE
        return t.init()
    }
    public static func test_foo4() {
        let c2_4: C2 = D0<C1>().foo4(C2.self) // Should compile ; does compile
        let c0_4: C0 = D0<C1>().foo4(C0.self) // Should not compile ; does not compile
        let cX_4: CX = D0<C1>().foo4(CX.self) // Should not compile ; does not compile
    }
}

第一个示例foo0是我期望的工作方式,但是没有编译。在第五个示例foo4中,我将通用参数P硬编码为C1,它应该在D0<C1>中解析,即我在每次测试中使用的方式。这可以按预期工作,但不再通用。

我断言foo0应该进行编译,并且(在D0<C1>下)具有与foo4相同的编译时行为。

1 个答案:

答案 0 :(得分:0)

因此,错误不正确。 ...对吗?

嗯...当前的约束系统还不够强大,无法支持这种约束。好消息是Generics Manifesto中描述了Swift路线图上的泛型超类型约束。

出于相同的原因,以下内容也不会编译:

func test<A, B>(_ a: A, _ b: B) where A: AnyObject, B: A {
    // ^^^  Type 'B' constrained to non-protocol, non-class type 'A'
}

清单中的示例都不是

protocol P {
  associatedtype Base
  associatedtype Derived: Base
}

不幸的是,您必须等到Swift可以使用此功能后,才能按需运行它。

相关问题