Error using associated types and generics

后端 未结 3 1208
刺人心
刺人心 2021-02-03 11:55

The following code gives me an error on line return p.foo(self). The error says: \'P\' does not have a member named \'foo\'.

protocol P         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 12:39

    You never required that the two Ts match. They are each in their own scope. Therefore the compiler looks for a foo function with the wrong parameter types.

    I believe something like this would be correct:

    protocol P {
        typealias T
        func foo(c: C) -> T
        func foo2() -> T
    }
    
    class C {
        var p: ConcreteP
        init (p: ConcreteP) {
            self.p = p
        }
        func bar() -> T {
            return p.foo(self);
        }
    }
    

    At least I don't get any syntax errors. However on compilation I get:

    error: unimplemented IR generation feature non-fixed class layout
        var p: ConcreteP
            ^
    LLVM ERROR: unimplemented IRGen feature! non-fixed class layout
    

    That sounds like a compiler bug.

提交回复
热议问题