如何为嵌套在通用结构中的类实现运算符?

时间:2014-06-09 19:10:58

标签: generics nested operators swift equality

当我在一个泛型结构中嵌入一个类并尝试实现相等运算符时,如下所示:

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

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

当我尝试运行项目时出现以下错误:

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

然而,当我在没有嵌套类的情况下做同样的事情时,它工作正常:

class MyClass : Equatable {}

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

这是编译器的错误,还是我做错了什么?

2 个答案:

答案 0 :(得分:4)

classstruct嵌套在泛型类型结构中,现在被XCode6 Beta6标记为无效

enter image description here

答案 1 :(得分:1)

您可以在单独的文件或空间中定义Inner类,定义其运算符,然后在内部类中创建该类型的var:

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

从beta 6开始