`fun foo() = println("No receiver") fun Int.foo() ...
# announcements
a
Copy code
fun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")
with(42) { foo() } // will print 'My receiver is 42' but I want 'No receiver'
Is it somehow possible to call the function without receiver, although one is given by the current scope?
b
if
foo()
is not top level fun, then you can specify it via `this`:
Copy code
with(42) { this@YourClassName.foo() }
v
Depends on more context. For example you can qualify it if it is a top-level function like
Copy code
package bar

fun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")

fun main(){
    with(42) { bar.foo() }
}
Or if the
foo
is a local function it will also win:
Copy code
fun Int.foo() = println("My receiver is $this")

fun main(){
    fun foo() = println("No receiver")
    with(42) { foo() }
}
A combination of the two:
Copy code
package bar

fun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")

fun main(){
    fun foo() = bar.foo()
    with(42) { foo() }
}
Or you can qualify the
this
reference if possible like
Copy code
class Foo {
    fun foo() = println("No receiver")
    fun Int.foo() = println("My receiver is $this")

    fun main(){
        with(42) { this@Foo.foo() }
    }    
}
fun main() = Foo().main()
a
Copy code
fun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")
Copy code
fun main() {
    with(42) { foo() } // will print 'My receiver is 42' but I want 'No receiver'
}
Sorry for being so scarce on the context. Both functions are top level declarations.
v
Anything wrong with the options I gave you?
a
Thank you. I guess I need to use the package name. Unfortunately when I tested this I used no package 🤣