I currently have to write
val myList: List<Int>? = listOf()
if(!myList.isNullOrEmpty()){
// myList manipulations
}
Which smartcasts myList to no non null. The below do not give any smartcast:
if(!myList.orEmpty().isNotEmpty()){
// Compiler thinks myList can be null here
// But this is not what I want either, I want the extension fun below
}
if(myList.isNotEmptyExtension()){
// Compiler thinks myList can be null here
}
private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean {
return !this.isNullOrEmpty()
}
Is there a way to get smartCasts for custom extensions?
For retention policies: On the Decide if you want to retain content, delete it, or both page, select Only delete items when they reach a certain age, and specify the time period.
If you need to initially retain content with the primary label (most typical): On the Define label settings page, select Retain items indefinitely or for a specific period and specify the retention period. Then on the Choose what happens after the retention period page, select Change the label > Choose a label.
When you configure a retention label or policy to retain content, you choose to retain items for a specific number of days, months (assumes 30 days for a month), or years. Or alternatively, retain the items forever.
This is solved by contracts introduced in Kotlin 1.3.
Contracts are a way to inform the compiler certain properties of your function, so that it can perform some static analysis, in this case enable smart casts.
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@ExperimentalContracts
private fun <T> Collection<T>?.isNotEmptyExtension() : Boolean {
contract {
returns(true) implies (this@isNotEmptyExtension != null)
}
return !this.isNullOrEmpty()
}
You can refer to the source of isNullOrEmpty
and see a similar contract.
contract {
returns(false) implies (this@isNullOrEmpty != null)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With