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

时间:2022-03-02 14:29:38

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?

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

2 个解决方案

#1


4  

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

现在,在XCode6 Beta6中将类或结构嵌套在泛型类型结构中时将其标记为无效

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

#2


1  

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:

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

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

As of beta 6

截至测试版6

#1


4  

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

现在,在XCode6 Beta6中将类或结构嵌套在泛型类型结构中时将其标记为无效

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

#2


1  

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:

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

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

As of beta 6

截至测试版6