kotlin-reified-type-parameters

How to have generic ViewModel in BaseActivty class

怎甘沉沦 提交于 2021-02-07 20:16:45
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

How to have generic ViewModel in BaseActivty class

守給你的承諾、 提交于 2021-02-07 20:14:27
问题 I want to have a base activity class that takes care of some initialization I started defining it like this. abstract class BaseActivity<VIEW_MODEL : ViewModel, BINDING : ViewDataBinding> : AppCompatActivity() { lateinit var viewmodel: VIEW_MODEL lateinit var binding: BINDING lateinit var glide: RequestManager @get:LayoutRes abstract val layoutResource: Int override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this,

How does the reified keyword in Kotlin work?

谁说我不能喝 提交于 2019-11-26 23:43:53
I'm trying to understand the purpose of the reified keyword, apparently it's allowing us to do reflection on generics . However, when I leave it out it works just as fine. Anyone care to explain when this makes an actual difference ? TL;DR: What is reified good for fun <T> myGenericFun(c: Class<T>) In the body of a generic function like myGenericFun , you can't access the type T because it's only available at compile time but erased at runtime. Therefore, if you want to use the generic type as a normal class in the function body you need to explicitly pass the class as a parameter as shown in

How does the reified keyword in Kotlin work?

久未见 提交于 2019-11-26 08:58:05
问题 I\'m trying to understand the purpose of the reified keyword, apparently it\'s allowing us to do reflection on generics. However, when I leave it out it works just as fine. Anyone care to explain when this makes an actual difference ? 回答1: TL;DR: What is reified good for fun <T> myGenericFun(c: Class<T>) In the body of a generic function like myGenericFun , you can't access the type T because it's only available at compile time but erased at runtime. Therefore, if you want to use the generic