Interface.k 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. invoke {
  2. interface I {}
  3. class A is I {
  4. init () {
  5. do nothing
  6. }
  7. unwrap() -> Int {
  8. return 0
  9. }
  10. }
  11. assert A() is I
  12. assert A is Impl<I>
  13. interface IntBox {
  14. unwrap () -> Int
  15. square_unwrap () -> Int {
  16. return unwrap() ^ 2
  17. }
  18. }
  19. assert A is not IntBox
  20. class B is I, IntBox {
  21. init (x: Int) {
  22. do nothing
  23. }
  24. unwrap () -> Int {
  25. return x
  26. }
  27. }
  28. assert B is Impl<I>
  29. assert B is Impl<IntBox>
  30. let b = B(3)
  31. assert b is I
  32. assert b is IntBox
  33. assert b->unwrap() == 3
  34. assert b->square_unwrap() == 9
  35. class C is B {
  36. init (x: Int) {
  37. mount B(x)
  38. }
  39. }
  40. assert C is Impl<I>
  41. assert C is Impl<IntBox>
  42. let c = C(3)
  43. assert c is I
  44. assert c is IntBox
  45. assert c->unwrap() == 3
  46. assert c->square_unwrap() == 9
  47. }