Operator overload feature is incomplete

  1. It is impossible to overload some possible operators, whithout any reason, for example, backslash.

  2. It is impossible to overload operator for existing classes, for example, for integers, because member methods supercede extesions methods

  3. It is impossible to overload operators within receiver context. For example, you cand define a+b so that it mean this.somefunction(a,b) inside receiver context.


Ad. 2. I think this is not really related specifically to operators. As you said yourself, it works like this for all functions, so for consistency, operators should behave the same way.

Also, it seems to me that giving higher priority to extension functions than to member functions would be very error-prone. But this is not a strong opinion.

Ad. 3. Either I misunderstood you or it works as expected:

fun main() {
    with(MyScope()) {
        println(Foo(5) + Foo(7))

class MyScope {
    operator fun Foo.plus(other: Foo) = Foo(foo + other.foo)

data class Foo(val foo: Int)

edit: Ad.3. Ok, I think you meant that you can’t rename the function that works as an operator. Well, you can create an operator and just invoke this function. Also, you can make it inline and then it will effectively work as renamed operator.

1 Like