thecipster
06/16/2017, 10:54 AMjava.io.File
variable is null or not readable. In Java this vould be
if (myFile == null || !myFile.canRead()) {
// error!
}
which works always because of short-circuit evaluation.
IntelliJ translates the above code to:
if (simDesc == null || !simDesc!!.canRead()) {
// error!
}
which is a bit ugly IMHO.
Any idea on making it more idiomatic?kirillrakhman
06/16/2017, 10:58 AMsimDesc
is not a local variable but a mutable property. either save the value to a local variable, make the property immutable (val
) or use some tricks like simDesc.takeIf { it.canRead() }?.let { /* do stuff */ }
uhe
06/16/2017, 11:03 AMBoolean
returning methods/properties on nullable objects is not very satisfyingthecipster
06/16/2017, 11:06 AMsimDesc
must be a mutable property (var simDesc: File? = null
) because Java library.
The trick with takeIf
is nice but hardly more readable š.
I think that in this case the compiler knows enough to avoid the !!
operator.uhe
06/16/2017, 11:08 AMsimDesc
is null on the second branchkirillrakhman
06/16/2017, 11:08 AMthecipster
06/16/2017, 11:09 AMkirillrakhman
06/16/2017, 11:09 AMif (simDesc?.canRead() != true)
but that won't help you, if you need to access the value againthecipster
06/16/2017, 11:13 AM!!
.
Thanks a lot! škirillrakhman
06/16/2017, 11:14 AM