max(_:_:) and min(_:_:) in an Int extension in Swift

723 views Asked by At

Why are max(::) and min(::) not allowed in a Swift Int extension, such as:

extension Int {
    func some(low: Int, high: Int) -> Int {
        return max(low, high)
    }
}

The error says: Static member 'max' cannot be used on instance of type'Int'

1

There are 1 answers

2
matt On BEST ANSWER

The compiler needs some help here. It thinks you mean https://developer.apple.com/documentation/swift/int/1540171-max

As Hamish says, you can disambiguate within this context to specify the global function by calling it Swift.max.