Not sure where to ask this :sweat_smile: Regarding Contracts, I was wondering if it’s possible (or w...
p
Not sure where to ask this πŸ˜… Regarding Contracts, I was wondering if it’s possible (or will ever be) to signal something about a parameter if a lambda is called. Like this:
This would mean that, if the block is run, then the subject is not null (or other condition)
l
I think
stuff?.let { }
does the same thing as what you want, do you have another use case?
βž• 1
p
Yeah that one does, however this case is a replacement for:
Copy code
receiver.run { stuff?.let { this.doSomethingAndReturnSameTypeAsReceiver(stuff) } ?: receiver }
Another use case if we could do:
Copy code
calls(block) implies (subject is AppCompatActivity)
Then we could define something like:
Copy code
fun Activity.ifAppCompat(block: () -> R): R {
  contract {
    calls(block) implies (this is AppCompatActivity)
  }
  if(this is AppCompatActivity) {
    block()
  }
}
That said, these could potentially be inferred by the compiler. But making it explicit will probably be more efficient (say someone that knows close to nothing about the Kotlin compiler πŸ˜… )
l
That AppCompat example is less abstract, but can't you simply add an
AppCompatActivity
receiver for the
block
lambda?
πŸ‘ 1