I'm trying to overload the % operator for doubles in Swift. I realize the reason that the two methods on the Double type exist, but truncatingRemainder()
's behavior works fine for my use case.
I'm using Xcode Playgrounds to work through this problem and I thought I had it solved.
infix operator %
func % (left: Double, right: Double) -> Double {
return left.truncatingRemainder(dividingBy: right)
}
var x = 0.0
for _ in 0...5 {
print(x)
x = (x + 1.5) % 5.0
}
print(x)
This works fine and gives me the correct expected behavior.
After this, I tried putting it into a library I am working on. I created a new .swift file, rebuilt the library, ensured the import statement was working (by using other functions/methods in my library) in my Playground, and I get the following errors:
- Ambiguous operator declarations found for operator
- Cannot convert value of type '()' to expected argument type 'Double'
- Operator is not a known binary operator
Can anyone explain the difference between putting it in my Playground and including it in an imported file?