This is my first day of learning Kotlin and I have code like this:
val tagInfo : UHFTAGInfo? = rfid.readTagFromBuffer()
if (tagInfo != null) {
val msg = hnd.obtainMessage()
msg.obj = tagInfo
hnd.sendMessage(msg)
}
Can I reduce it by one line by obtaining a value from rfid.readTagFromBuffer()
and checking it for null?
In modern C# we can do it like this:
if (rfid.readTagFromBuffer() is UHFTagInfo tagInfo){
val msg = hnd.obtainMessage()
msg.obj = tagInfo
hnd.sendMessage(msg)
}
I have tried to find answer here:
https://kotlinlang.org/docs/typecasts.html
but no success. So I assume Kotlin does not offer such feature, but maybe there is something similar that can reduce these two lines to one?