Parameter and method precedence

I had this (very simplified) code:

// In a generic lib
interface ISerializer {
	//...
}

// In a project using this lib
class MySerializer : ISerializer {
	fun writeMyBlock(name: String, exp: ISerializer.() -> Unit) {
		//...
		exp()
		//...
	}
}

Months later, in the generic lib, I simply add a method in my interface (unfortunately, I choose exp as name):

// In a generic lib
interface ISerializer {
	fun exp(vararg s: String) {
		//...
	}
}

Some of my projects are then silently buggy: The call to exp() in writeMyBlock() no longer calls the function parameter, but the interface method ISerializer.exp(). No warnings nor compilation error.

Precedence of object method over local function parameters seems an issue, making the code unstable in large projects.

3 Likes

Ohh, interesting. Apparently, this is somehow connected to the fact the lambda is an extension over ISerializer. If we make it simply () -> Unit then the lambda is preferred. Maybe this is due to general rule that members have precedence over extensions. But I agree, this is definitely unexpected.

4 Likes