I just played around a bit with extension methods in Kotlin. So I created some class Foo with an instance variable bar. Then I added a Kotlin file into the same namespace as Foo with an extension method printIt that prints Foo’s bar to the console.
Now I changed the visibility of bar to protected. As a result the extension method printIt does not compile any more, because it can’t see bar any more. That surprised me. Class Foo and the Kotlin file that adds an extension method to it are in the same namespace.
I checked this out with Scala using their implicit conversion mechanism and it was the same thing. So I guess it must be a restriction of the JVM. Unhappily, this restriction does not make sense. Classes/files in the same package should be able to see each other’s vars and methods if they are declared public or protected. I checked it out in Swift where this is the case, see https://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/
Any chance this can be backed up in the future in Kotlin or definitely a JVM issue?