Why this: ```class Foo fun Foo.test() { print...
# intellij
m
Why this:
Copy code
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
scratch files, and other kotlin scripts, are not quite the same thing as a normal source file
you can imagine the whole scratch file being wrapped sorta like a
Copy code
object {
    class Foo
    fun Foo.test()
}
and because
test
is a member of the wrapper and a extension of
Foo
, you get that error
it would be nice if some of these limitations were better documented, https://youtrack.jetbrains.com/issue/KT-11873/Document-allowed-contents-of-Kotlin-scripts-and-the-REPL
m
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
Why is the wrapping necessary?
e
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