Marc Knaup
05/25/2020, 6:22 AM// base module provides registry
interface Feature
private val availableFeatureTypes = hashSetOf<String>()
inline fun <reified TFeature: Feature> registerFeature(feature: TFeature) {
availableFeatureTypes.add(typeNameOf<TFeature>())
// …
}
inline fun <reified TFeature: Feature> isFeatureAvailable() =
availableFeatureTypes.contains(typeNameOf<TFeature>()) // no 'NoClassDefFoundError' because inlined
// a module providing `SomeFeature` registers it by fully qualified name
object SomeFeature: Feature
fun someFunThatGetsCalledIfModuleIsUsed() {
registerFeature(SomeFeature)
}
// another module can optionally depend on SomeFeature
if (isFeatureAvailable<SomeFeature>()) { // no 'NoClassDefFoundError' because inlined
val feature = SomeFeature() // executed only if SomeFeature is available
// …
}
World that approach work in Kotlin Native or would it fail because everything is resolved at compile-time even if the code is never executed?