Astronaut4449
11/10/2020, 10:40 AMfun 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?bezrukov
11/10/2020, 10:49 AMfoo()
is not top level fun, then you can specify it via `this`:
with(42) { this@YourClassName.foo() }
Vampire
11/10/2020, 10:52 AMpackage 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:
fun Int.foo() = println("My receiver is $this")
fun main(){
fun foo() = println("No receiver")
with(42) { foo() }
}
A combination of the two:
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
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()
Astronaut4449
11/10/2020, 11:01 AMfun foo() = println("No receiver")
fun Int.foo() = println("My receiver is $this")
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.Vampire
11/10/2020, 11:02 AMAstronaut4449
11/10/2020, 11:18 AM