The following code causes an error: `protected pro...
# compiler
e
The following code causes an error:
protected property exposes its internal type BarImpl
even though
FooImpl
isn't open. Since
bar
is protected, only subclasses of
FooImpl
could access it, but that isn't possible because
FooImpl
is final. So why is there an issue with making
FooImpl
public?
Copy code
abstract class Foo<B : Bar> {
  protected abstract val bar: B
}

class FooImpl : Foo<BarImpl> {
  override val bar: BarImpl
}

internal class BarImpl : Bar
k
The problem here is not just the
val bar
property. Even if you remove this property, there is still a problem with the fact that
BarImpl
is a type parameter in the abstract class
FooImpl
extends. Consider this:
Copy code
// MODULE A

interface Bar

abstract class Foo<B : Bar>

class FooImpl : Foo<BarImpl>()

internal class BarImpl : Bar

// MODULE B

inline fun <reified T: Bar> foo(x: Foo<T>) {
    println(T::class)
}

fun main() {
    foo(FooImpl())
}
Now if there was no compiler error, and
fun foo
and
fun main
ran in another module, you'd be able to see an internal class from another module.
e
I thought that would be an issue but there was no error about it (only the property). However putting that code in Kotlin playground does show an error in the type parameter. In any case, if I change
Foo
to
Copy code
class FooImpl : Foo<Bar>()
then there's no error about the type parameter anymore, but there is still one about the property.