Mount.k 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. invoke {
  2. class Delegate {
  3. init (f: Callable) {
  4. do nothing
  5. }
  6. work () -> Int {
  7. return f() + 1
  8. }
  9. }
  10. class A is Delegate {
  11. init (x: Int) {
  12. let d = mount Delegate(f)
  13. }
  14. private f () -> Int {
  15. return x
  16. }
  17. double_work () -> Int {
  18. return (d->work())^2
  19. }
  20. }
  21. let a = A(9)
  22. assert a is A
  23. assert a is Delegate
  24. assert a -> work() == 10
  25. assert a -> double_work() == 100
  26. class B is A {
  27. init (x: Int) {
  28. mount A(x)
  29. }
  30. }
  31. let b = B(9)
  32. assert b is B
  33. assert b is A
  34. assert b is Delegate
  35. assert b -> work() == 10
  36. assert b -> double_work() == 100
  37. class C { init() {} }
  38. class D {
  39. init () {
  40. do nothing
  41. }
  42. hello () -> String {
  43. return 'Hello'
  44. }
  45. }
  46. class E is A, C, D {
  47. init (x: Int) {
  48. mount C()
  49. mount A(x)
  50. mount D()
  51. }
  52. }
  53. let e = E(1)
  54. assert e is A
  55. assert e is C
  56. assert e is D
  57. assert e -> work() == 2
  58. assert e -> hello() == 'Hello'
  59. }