https://kotlinlang.org logo
Title
u

ursus

10/09/2019, 7:26 PM
Foo is final, I dont get it
r

Ruckus

10/09/2019, 8:04 PM
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

ursus

10/09/2019, 8:44 PM
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

Ruckus

10/09/2019, 8:48 PM
Aren't there fields or functions that use the generic param? If not, why is there a generic?
u

ursus

10/09/2019, 8:49 PM
basically its this
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

Ruckus

10/09/2019, 8:53 PM
Because I can call
FooFragment.onCreateViewModel()
u

ursus

10/09/2019, 8:54 PM
how? its protected
You cant, im trying it right now
r

Ruckus

10/09/2019, 8:55 PM
Yes, but that's kind of beside the point. There's no way the compiler can understand that.
u

ursus

10/09/2019, 8:56 PM
Compilers dont understand visibility?
isnt this some sort of javainterop bs? I think protected is also package private in java
r

Ruckus

10/09/2019, 8:58 PM
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

ursus

10/09/2019, 9:01 PM
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

Ruckus

10/09/2019, 9:03 PM
But private only works withing the same class/file. Protected can jump between libraries.
u

ursus

10/09/2019, 9:05 PM
im still not sure why does the param need to be public, im trying even simpler example
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
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)