Infix function in receivers


#1

class A {

infix fun doSomething(string: String) {}

fun example() {
	doSomething "" //compile error

	doSomething("") //allowed, but without infix syntax

	this doSomething "" //allowed, but the "this" should be innecesary
}

}

Shouldn’t the first case be accepted by the compiler?

For any other function, if you are inside a class you don’t need to use this keyword,
the same happens with extension functions and lambdas.


#2

I guess it has to do with the way infix functions work.
Infix only works with exactly one argument (or 2 if you count the receiver) so the function name can be placed between the 2 arguments.
IMO removing the first argument looks strange and it is not really intuitive to understand. The normal infix case is easy to understand as we are used to it from normal operators. And adding additional “operators” and similar functions is what I feel infix should be used for.


#3

Yeah, I found out the same using infix with DSLs. The “this” sould be superfluous here…?


#4

An infix function always requires the left-hand and the right-hand side arguments. Making it possible to omit one would lead to numerous ambiguities in parsing.


#5

Sure,
but isnt the kontext of the receiver (this) automatically on the left side?


#6

It is, but it doesn’t matter. The syntax of an infix expression does not allow the left hand side operand to be omitted, even if it can be omitted in other contexts.


#7

Duplicate thread?