Nested classes or structs cannot use protocols in methods


(Stellar) #1
class Test {
    func test() {
        struct NestedTest: Comparable {
            static func < (lhs: NestedTest, rhs: NestedTest) -> Bool {
                return false;
            static func == (lhs: NestedTest, rhs: NestedTest) -> Bool {
                return false;

error: Type 'NestedTest' does not conform to protocol 'Comparable'

Is it a bug or does not support this?

(Maik) #2

This is because the Comparable protocol has a Self constraint. I'm not sure if it is a bug.

if you add this

print("\(String(reflecting: NestedTest.self))") 

to your test method you will get something like this

__lldb_expr_1.(unknown context at 0x1005403bc).NestedTest 

So there is definitely an issue when swift tries to resolve the type. I'm just not sure if it is intended or not.

(Anthony Latsis) #3

The fourth time this is asked about this week; apparently a recently introduced issue. Here's the SR-8696

(Stellar) #4

Thank you for your answer. I want to try the sample you provided, but can't compile?How to get the output you gave?

(^) #5

why wasn’t this caught in the source compatibility suite?

(Anthony Latsis) #6

Types nested in functions that explicitly conform to protocols with an operator function requirement is quite the specific case, I'm not surprised there isn't a compiler test for that. Regarding the source compatibility suite, it could be that the changes in the PR that introduced the bug didn't require testing source compatibility. But I can't be sure there is an appropriate test in the source compatibility suite either; that might also be the case, assuming there is high probability that the source compatibility tests were run at least once after introducing the bug.

(Jordan Rose) #7

I'm pretty sure there's no test for it because it has never worked. I'm not sure why it's getting so much attention right now.