Nadohs / Cast-Free-Arithmetic-in-Swift

Cast-free arithmetic operations in Swift 2.0!
37 stars 3 forks source link

Question for bitwise operator #5

Open oozoofrog opened 8 years ago

oozoofrog commented 8 years ago

Thanks. this source is very brilliant.

I have a question for bitwise - like or(|) - operator overloading.

First time, i just added function like,

public func | <T:IntNumberConvertible, U:IntNumberConvertible,V:IntNumberConvertible>(lhs: T, rhs: U) -> V {
        return lhs.operate(rhs, combine: |)
}

but, sadly, this is return ambiguous error on compile

so i added few more codes like,

extension IntNumberConvertible {
    private typealias CombineIntType = (UInt, UInt) -> UInt
    private func operate<T:IntNumberConvertible,V:IntNumberConvertible>(b:T, combine:CombineIntType) -> V{
        let x:UInt = self.convert()
        let y:UInt = b.convert()
        return combine(x,y).convert()
    }
}

public func | <T:IntNumberConvertible, U:IntNumberConvertible,V:IntNumberConvertible>(lhs: T, rhs: U) -> V {
    return lhs.operate(rhs, combine: |)
}

and, it's working.

I hate another extension and overloading. can i have more efficient way?