Nested struct Comparable conformance not working?


(^) #1

why does this not work when the struct is local to a function?

func f() 
{
    struct Pair:Hashable, Comparable
    {
        let x:Int, y:Int 
        
        static 
        func < (lhs:Pair, rhs:Pair) -> Bool 
        {
            if lhs.y < rhs.y 
            {
                return true 
            }
            else if lhs.y == rhs.y 
            {
                return lhs.x < rhs.x
            }
            else 
            {
                return false
            }
        }
    }
}
:3:12: error: type 'Pair' does not conform to protocol 'Comparable'
    struct Pair:Hashable, Comparable
           ^

Nested classes or structs cannot use protocols in methods
Type scope operators doesn't work if type is declared in function scope, bug?
Type scope operators doesn't work if type is declared in function scope, bug?
(Anthony Latsis) #2

Certainly a bug related to operator functions in conformances. IteratorProtocol and a simple custom protocol work fine. Using a name instead of < or having a default implementation works too.

protocol P { static func < (lhs: Self, rhs: Self) -> Bool }

func f() {
  struct B: P {
    static func < (lhs: B, rhs: B) -> Bool {return true} // The error
  }
}

(Ole Begemann) #3

This looks like the same bug as the one that's being discussed here: Hashable struct in function.


(Anthony Latsis) #4

@Max, have you filed the bug with the Hashable struct inside a function yet?