Hello, can somebody explain why this doesn’t compi...
# codereview
n
Hello, can somebody explain why this doesn’t compile:
Copy code
class A {
    fun String.hello() {}
}

fun String.hello2() {
    A().hello() // cannot find hello()
}
However this compiles:
Copy code
class A {
    fun String.hello() {}
}

fun String.hello2() {
    with(A()) {
        hello()
    }
}
When I try and inline the
with
call it doesn’t compile again.
w
fun String.hello() {}
it is an extension to be called on a
String
A().hello()
,
A
isn't a
String
, so you can't call
hello
.
fun String.hello2() {
you can call the
hello
there, because it will be called on the
String
of
hello2
.
Similar of having this:
Copy code
class A {
    fun hello(valueHello: String) {}
}

fun hello2(valueHello2: String) {
    with(A()) {
        hello(valueHello2)
    }
}
m
The problem resides in the fact that String.hello() is available only inside A, or when an object of type A is the receiver, so that works in with() because you’re changing the receiver (this) inside the block.
n
I guess I have to wait for multiple receivers to get this code to compile?
k