I’d like a convenience extension to return true wh...
# getting-started
a
I’d like a convenience extension to return true when a nullable string is not null, and not blank — neither should be processed. After the extension returns true for an immutable, I don’t want to have to have to
!!
it. But it seems I can’t use “this” in a contract:
Copy code
contract {
        returns(true) implies (this != null)
    }

    return !this.isNullOrBlank()
Is there a way to do this?
y
you need to use a qualified this instead, and so assuming your function is called
myCastAsNullOrBlankFun
, then your contract should be this:
Copy code
contract {
    returns(true) implies (this@myCastASNullOrBlankFun != null)
}
n
Why you use a contrat ?
Copy code
fun String.checkNotNull(): Boolean{
    return !isNullOrBlank()
}
e
fun String.isNullOrBlank()
itself has a contract,
Copy code
contract {
    returns(false) implies (this@isNullOrBlank != null)
}
not clear why you would need another function to wrap it, but for sure the
this
you wrote is not referring to what you think it is - it's
this@contract
a
@Youssef Shoaib [MOD] perfect, thanks!
@Nicolas B, I would prefer a contract so that after I prove my immutable value is not null, I don’t need
!!
, or anything else — I just want a custom smart-cast to non-nullable