class Foo
fun Foo.test() {
println("hello")
}
val x = Foo::test
x(Foo())
doesn’t work in an IntelliJ/AndroidStudio Kotlin scratch file? It shows: “‘test’ is a member and an extension at the same time. References to such elements are not allowed” error. But it works in Kotlin playground: https://pl.kotl.in/ubsrNwrCx ? Is this a bug?
e
ephemient
11/22/2022, 7:44 PM
scratch files, and other kotlin scripts, are not quite the same thing as a normal source file
ephemient
11/22/2022, 7:46 PM
you can imagine the whole scratch file being wrapped sorta like a
Got it, thanks for the explanation. Yeah I agree that it should be at least documented. It's almost impossible to figure this out if you don't know that there may be differencies. I wish they lift these restrictions at some point. I'll remember not to trust scratch files from now on 😄 Thanks a lot!
e
Emil Kantis
11/22/2022, 7:59 PM
Why is the wrapping necessary?
e
ephemient
11/22/2022, 8:26 PM
I can't say for sure, it's probably an implementation detail primarily, but it is how the scripting host can provide context to the script (e.g. Gradle provides
project
etc. to
*.gradle.kts
scripts) which is an important use-case for Kotlin scripting, even if it's not what you want in the simple REPL and scratch file case