How do I implement an operator for a class nested in a generic struct?

半世苍凉 提交于 2019-12-04 16:00:13

问题


When I nest a class inside a generic struct and try to implement the equality operator, like this:

struct Outer<T> {
    class Inner : Equatable {}
}

@infix func == <T>(lhs: Outer<T>.Inner, rhs: Outer<T>.Inner) -> Bool {
    return lhs === rhs
}

I get the following error when I try to run the project:

While emitting IR SIL function @_TFCC4Test5Outer5InnerCU__fMS1_FT_S1_ for 'init' at .../Testing.swift:20:11
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

However, it works fine when I do the same thing without nesting the class:

class MyClass : Equatable {}

@infix func == (lhs: MyClass, rhs: MyClass) -> Bool {
    return lhs === rhs
}

Is this a bug with the compiler, or am I doing something wrong?


回答1:


Nesting a class or struct in a generic type struct is now flagged as invalid by XCode6 Beta6




回答2:


You could define the Inner class in a separate file or space, defining its operator then make a var of that type in your inner class:

class Inner: Equatable {}
func == (left: Inner, right: Inner) -> Bool {
    return true
}
struct Outer {
    var myVar: Inner
}

As of beta 6



来源:https://stackoverflow.com/questions/24127250/how-do-i-implement-an-operator-for-a-class-nested-in-a-generic-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!