louiscad
08/11/2018, 8:46 AM/** Matches [android.support.v7.app.AppCompatViewInflater.createView] content. */
val appCompatViewFactory = { clazz: Class<out View>, context: Context, _: Style<out View>? ->
when (clazz) {
TextView::class.java -> AppCompatTextView(context)
Button::class.java -> AppCompatButton(context)
ImageView::class.java -> AppCompatImageView(context)
EditText::class.java -> AppCompatEditText(context)
...
else -> null
}
}
The issue is that there's no type check, I could return anything for anytype and the compiler won't fail. I want to make sure each when branch returns an object of the type of the case on which it matched.Andreas Sinz
08/11/2018, 9:02 AMlouiscad
08/11/2018, 9:03 AMAndreas Sinz
08/11/2018, 9:04 AMlouiscad
08/11/2018, 9:07 AMClass<*> and smartcasting checks on its instances (like TextView::class.java in my example). This would require me to make my function generic to benefit from this.arekolek
08/11/2018, 9:08 AMinline fun <reified T : View> factory(clazz: Class<T>, context: Context) = when (clazz) {
TextView::class.java -> AppCompatTextView(context)
Button::class.java -> AppCompatButton(context)
ImageView::class.java -> AppCompatImageView(context)
EditText::class.java -> AppCompatEditText(context)
else -> throw RuntimeException()
} as Tlouiscad
08/11/2018, 9:10 AMAny? where a TextView or other specific type was expectedarekolek
08/11/2018, 9:15 AMas T point?louiscad
08/11/2018, 9:17 AMlouiscad
08/11/2018, 9:25 AMwhen on reified types https://youtrack.jetbrains.com/issue/KT-26048louiscad
08/11/2018, 9:31 AMreified on non inline functions that have a Class or KClass parameter: https://youtrack.jetbrains.com/issue/KT-26049Andreas Sinz
08/11/2018, 9:40 AMTextView the same class that AppCompatTextView returns?louiscad
08/11/2018, 9:44 AMAppCompatTextView is a TextViewAndreas Sinz
08/11/2018, 9:48 AMAndreas Sinz
08/11/2018, 9:49 AMlouiscad
08/11/2018, 9:55 AMAndreas Sinz
08/11/2018, 10:36 AMlouiscad
08/11/2018, 4:43 PMClass<V>)louiscad
08/11/2018, 4:45 PM