ursus
05/19/2026, 7:22 PMclass IntentOpener(private val activity: Activity?) {
private fun startActivity(intent: Intent): Boolean {
val activity = activity ?: return false <-------------------
activity.whatever()
return true
}
}
vs
class IntentOpener(private val activity: Activity?) {
private fun startActivity(intent: Intent): Boolean {
if (activity == null) return false <-------------------
activity.whatever()
return true
}
}
Which variant is more idiomatic? First one shadows but doesn't rely on smart castsYoussef Shoaib [MOD]
05/19/2026, 7:25 PMwhatever returns:
private fun startActivity(intent: Intent): Boolean {
activity?.whatever() ?: return false
return true
}ursus
05/19/2026, 7:26 PMYoussef Shoaib [MOD]
05/19/2026, 7:27 PMactivity was a var, then sure (although my variant still works then)ursus
05/19/2026, 7:29 PM