3

In Kotlin, we have infix

e.g. when we have

fun Int.test(value: Int) {}

We can use

1.test(2)

And when we put infix

infix fun Int.test(value: Int) {}

We can use as

1 test 2

For a class, the below is okay

class myclass {
    fun main() {
        test(1)
    }
    fun test(value: Int) {}
}

But with infix the below is not okay

class myclass {
    fun main() {
        test 1
    }
    infix fun test(value: Int) {}
}

Apparently, it has to have

class myclass {
    fun main() {
        this test 1
    }
    infix fun test(value: Int) {}
}

Can I omit this, since test is call within the class itself?

Elye
  • 53,639
  • 54
  • 212
  • 474
  • Related: https://stackoverflow.com/questions/35976405/infix-notation-and-with-does-not-work-as-i-expected – BakaWaii Oct 08 '17 at 11:11
  • Possible duplicate of [Infix notation and with(...) does not work as I expected](https://stackoverflow.com/questions/35976405/infix-notation-and-with-does-not-work-as-i-expected) – zsmb13 Oct 08 '17 at 12:06

1 Answers1

5

It can't be omitted, you always need a left operand when using infix functions, which is this in your case:

"receiver functionName parameter"

There's no way around it.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196