Why does `Foo : Bar<Quax>` require `Quax` to...
# getting-started
u
Why does
Foo : Bar<Quax>
require
Quax
to be public, i.e. internal not allowed?
d
I think it's because inheritance is public.
c
Is
Foo
public? I would guess that the type parameter needs to have the same level of access as the class itself.
u
Yes foo is public
Inheritance is public? I dont get what you mean
d
In C++, you can have private inheritance. Where only the class know it has implemented an interface.
u
I dont think thats the issue here, issue is the visibility of the generic param, and also that its different from java
d
Inheritance in Kotlin/Java is public. So anything that has access to
Foo
will see that it inherits
Bar
. Since
Bar
has a generic parameter, the parameter would have to be visible to anything that has access to
Foo
. So if
Foo
is public, anything it exposes has to be
public
. If something has access to
Foo
, it won't know what
Quax
is, since it's internal. I hope this makes sense.
u
I dont think you are correct, because package private Quax works in java
d
And
Foo
can be used outside the package?
u
yep
works np
Copy code
public class Rah extends Meh<Ohe> {
}

class Ohe {
}

public abstract class Meh<T> {
}
d
Yup. Sounds like I'm wrong. Still weird to see. Perhaps Kotlin handles generics differently or something.
u
yea idk; thanks anyways
although, by the same logic private Ohe should work too, but doesnt, something is fishy
t
@Dominaezzz you are actually right, Roman explains the reason for the choice in Kotlin (Java behaves differently) here https://discuss.kotlinlang.org/t/why-does-kotlin-prohibit-exposing-restricted-visibility-types/7047/6
though, truth be told, it looks a bit inconsistent to me Does not compile:
Copy code
abstract class Bar<T>
internal class Quax
class Foo : Bar<Quax>
compiles:
Copy code
interface Bar<T> //visibility here does not matter, it always compiles
internal class Quax
class Foo : Bar<Quax>
u
huh really? weird
c
When you extend a class you need access to the internals when you implement interface there are no internals you need access to. The compiler isn't going to wait for your extended class to access something it can't have access to before complaining so it complains when you try to extend in a way that may cause problems.