Jiddles
07/19/2018, 2:03 PM// The below code results in a compilation error
data class ViewModel(
var lineSpeed: Int? = null
)
val viewModel = ViewModel()
if(viewModel.lineSpeed != null && viewModel?.lineSpeed > 0 ) {}
//Error : Smart cast to 'Int' is impossible, because 'viewModel.lineSpeed' is a mutable property that could have been changed by this time
Lucas Ł
07/19/2018, 2:08 PM!!
when accessing the (mutable/nullable) variableviewModel.lineSpeed?.let{
if(it>0) ....
}
arekolek
07/19/2018, 2:10 PMviewModel.lineSpeed?.takeIf { it > 0 }?.let {
}
Sam Woodall
07/19/2018, 2:12 PMfun Int?.isGreaterThan(a: Int) = this != null && this > a
if (viewModel.lineSpeed.isGreaterThan(3)) {
}
robstoll
07/19/2018, 2:28 PMJiddles
07/19/2018, 3:27 PM