TwoClocks
07/22/2020, 6:04 PMKClass
that extends a sealed class. Is there anyway to specify this at compile-time, or is this a run-time check?TwoClocks
07/22/2020, 6:04 PMT extends MySealedBaseType
or some such.streetsofboston
07/22/2020, 6:06 PMsealed
keyword is not part of the type of the class. If you do fun <T: SomeClass> myFunction(param: T)
, the compiler can’t guarantee that SomeClass
is a sealed class.
So, yes, you’d need run-time reflection (on the param
in the example I gave)TwoClocks
07/22/2020, 6:07 PMstreetsofboston
07/22/2020, 6:10 PMSomeClass
is a sealed class SomeClass
, then your myFunction
is fine.
But if you would remove the ‘sealed’ part (class SomeClass
), your definition of myFunction
would still compile fine.TwoClocks
07/22/2020, 6:12 PMMyBaseType
streetsofboston
07/22/2020, 6:13 PM<T : SomeClass>
part ensures that T
is either a SomeClass
or a sub-class of SomeClass
.TwoClocks
07/22/2020, 6:14 PMJoão Gonçalves
07/22/2020, 6:14 PMstreetsofboston
07/22/2020, 6:17 PMT
in the JVM (and Kotlin) is erased as a plain SomeClass
during runtime.
I think that Jonross’ question was more about if the compiler could check if a type-parameter is a sealed class or not when provided to a function or a class-instantiation.streetsofboston
07/22/2020, 6:19 PMKClass#isSealed
property, if you need to check this)Nir
07/22/2020, 6:21 PMNir
07/22/2020, 6:21 PMNir
07/22/2020, 6:22 PMNir
07/22/2020, 6:22 PM