The mod
method (see this) isn’t infix, and yet it would me much easier to use that way
Is there any reasons not to infix it ?
Modulo operator can be overloaded by using remaining operator.
I think the mod documentation lacks a @see rem
tag and a short description of why rem is favored over mod.
A simple Kotlin example :
fun main() {
print("Hello" % 2)
}
operator fun String.rem(charCount : Int) = length % charCount
Thank you, but I wasn’t talking about the remaining operator, I was talking about the mod method. Those are different methods.
The %
operator was originally overloaded using operator mod
. This was changed to operator rem
in Kotlin 1.1. I assume that the mod
function now exists mainly for backwards compatibility with old code that invokes mod
without using the operator syntax, as well as for binary compatibility with code compiled before Kotlin 1.1.
2 Likes