Advertisement
marcgruita

kotlin contracts

May 29th, 2024
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.54 KB | None | 0 0
  1. import kotlin.contracts.ExperimentalContracts
  2. import kotlin.contracts.contract
  3.  
  4. open class Base
  5.  
  6. class ChildOne: Base() {
  7.     fun childOneFunc() {
  8.         println("salut")
  9.     }
  10. }
  11.  
  12. class ChildTwo: Base()
  13.  
  14. @OptIn(ExperimentalContracts::class)
  15. fun funcThatExitsIfNotChildTwo(inst: Base) {
  16.     contract {
  17.         returns() implies (inst is ChildOne)
  18.     }
  19.  
  20.     if (inst !is ChildOne) {
  21.         error("nu e")
  22.     }
  23. }
  24.  
  25. fun main() {
  26.     val inst: Base = ChildOne()
  27.     funcThatExitsIfNotChildTwo(inst)
  28.  
  29.     inst.childOneFunc()
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement