Kirill Shepelev
05/04/2020, 7:12 PMinterface Animal
class Dog : Animal
class Cat : Animal
fun randomAnimal(callback: (Animal) -> Unit) {
callback.invoke(Dog())
}
fun main() {
// Directly casting parameter to specific type when
// declaring lambda parameters
randomAnimal { animal as Dog -> // pay your attention. Here we cast Animal to Dog
// Do something with dog...
}
// Safe cast sample
randomAnimal { animal as? Dog -> // safe cast Animal to Dog
animal?.let {
// Do something with dog...
}
}
}
Dominaezzz
05/04/2020, 8:23 PMKirill Shepelev
05/04/2020, 8:45 PMsetOnShowListener
from which we receive some DialogInterface
object and we exactly know that this is of type BottomSheetDialogFragment
. And we need to declare another one variable just to cast this parameter to type we want.
setOnShowListener { dialog ->
val bottomSheetDialog = dialog as BottomSheetDialog
}
It looks ugly for several reasons. First of all, we need to declare another one variable. It causes some problems with naming. After that we have dialog
variable in lambda scope that is unnecessary.
And if we compare the sample above with this one:
setOnShowListener { dialog as BottomSheetDialog ->
// ...
}
It looks much better IMHO.jw
05/04/2020, 8:49 PMval dialog = dialog as BottomSheetDialog
.Dominaezzz
05/04/2020, 8:49 PMsetOnShowListener { dialog ->
dialog as BottomSheetDialog
}
I'd also argue that this is an issue with DialogFragment
for not giving it a more specific type.jw
05/04/2020, 8:50 PMval input = input.toInt()
Dominaezzz
05/04/2020, 8:50 PMjw
05/04/2020, 8:53 PMCody Engel
05/04/2020, 9:08 PM"12345".forEach { character as String ->
character.toInt()
}
Being able to redefine would be nifty too. I think Android may have it fixed now, but I know several months ago I had a number of composite views with nonNullContext ->
scattered throughout, being able to just use context
within that scope would have been nice.jw
05/05/2020, 1:09 AMCody Engel
05/05/2020, 2:04 PMtoString()
worked that casting would work for that, but I guess not 🤷🏻♂️Cody Engel
05/05/2020, 2:05 PMas
would reference that function.