Foo is final, I dont get it
# getting-started
u
Foo is final, I dont get it
r
It's not about extending it, it's about the fact that anyone that has access to
Foo
has to have access to its generic types.
u
No it doesnt because generic param is not part of any public api, that is exactly what confuses me. Callsite cant see Quax in any form
unless im missing something
r
Aren't there fields or functions that use the generic param? If not, why is there a generic?
u
basically its this
Copy code
class FooFragment : BaseFragment<FooViewMode> {

	override fun onCreateViewModel() {
		return FooViewModel()
	}
}

class BaseFragment<T> {
	protected lateinit var viewModel: T

	override fun onCreateView(view: View) { <-- framework
		viewModel = onCreateViewModel()
	}

	protected abstract fun onCreateViewModel() : T
}
i.e. FooViewModel is implementation detail of FooFragment, I see no reason to expose it publicly; viewModel reference is protected inside BaseFragment hierarchy..idk
r
Because I can call
FooFragment.onCreateViewModel()
u
how? its protected
You cant, im trying it right now
r
Yes, but that's kind of beside the point. There's no way the compiler can understand that.
u
Compilers dont understand visibility?
isnt this some sort of javainterop bs? I think protected is also package private in java
r
They can't understand visibility of completely unrelated things. The fact that your function is private has nothing to do with the generic parameter. For example: if that class comes from an external library, and the function gets changed to public, anyone using your code somehow magically needs access to an object they can't see.
The generic type and the function visibility have nothing to do with each other.
This has nothing to do with Java
u
I dont get it what you mean, if it gets change to public it wont compile, same how if private function returns a private class, and you change it to public it wont compile
r
But private only works withing the same class/file. Protected can jump between libraries.
u
im still not sure why does the param need to be public, im trying even simpler example
Copy code
class Bar : Foo<Quax>() 

abstract class Foo<T>

internal class Quax
error is
public sublcass exposes internal supertype argument
Btw it works in java with package private T_T
Copy code
public class Rah extends Meh<Ohe> {
}

class Ohe {
}

abstract public class Meh<T> {
}
this is precisely what I want in kotlin (swap package private for internal)
it only complains if Ohe is private (nested)