I'm having a small issue with functional/SAM inter...
# getting-started
n
I'm having a small issue with functional/SAM interface, and I'm not sure if this is intended behavior, and if it is I don't understand why. If I convert this to an object extending
Example
and manually implement
example1
I can reference
example2
just fine, which puzzles me as to why the following snippet doesn't compile.
Copy code
val example = Example {
	// this is without `example1`
	example2() // this line doesn't compile
}

fun interface Example {
	fun example1()

	fun example2() {
		println("Example")
	}
}
d
When you pass/implement fun interface with a lambda, the compiler performs the SAM conversion: it takes the lambda, analyzes it and then converts it to the object of fun interface. Note the order: analysis first, then conversion, not vice versa. So at the moment of the analysis it's still unknown about member scope of the object which will be created later.
e
in a regular lambda,
this
is whatever it was from outside the lambda (unless it's inferred to take a receiver), not the lambda itself. same in a SAM-converted lambda
n
@dmitriy.novozhilov Yes, that does make perfect sense, and is what I suspected to be the reason. Do you have any idea if there are plans to reify this, to make it possible to call instance methods? I'd assume this is not likely to happen because it could be a breaking change in some scenarios.
d
There are no plans for this because • use-case is quite niche, so the workaround with anonymous object is acceptable • argument of
@ephemient
is fully valid, and it's better to not change the language semantics for this feature
y
Copy code
val example = Example {
	example2()
}

fun interface Example {
	fun Example.example1DontCall()

	fun example2() {
		println("Example")
	}
}
fun Example.example1() = example1DontCall()
This workaround exists