tmultimjs.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. discard """
  2. output: '''
  3. 7
  4. Hi derived!
  5. hello
  6. '''
  7. """
  8. # tmultim1
  9. type
  10. Expression = ref object {.inheritable.}
  11. Literal = ref object of Expression
  12. x: int
  13. PlusExpr = ref object of Expression
  14. a, b: Expression
  15. method eval(e: Expression): int {.base.} = quit "to override!"
  16. method eval(e: Literal): int = return e.x
  17. method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b)
  18. proc newLit(x: int): Literal =
  19. new(result)
  20. result.x = x
  21. proc newPlus(a, b: Expression): PlusExpr =
  22. new(result)
  23. result.a = a
  24. result.b = b
  25. echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7
  26. # tmultim3
  27. import mmultim3
  28. type TBObj* = object of TObj
  29. method test123(a : ref TBObj) =
  30. echo("Hi derived!")
  31. var aa: ref TBObj
  32. new(aa)
  33. myObj = aa
  34. testMyObj()
  35. # tmultim4
  36. type Test = object of RootObj
  37. method doMethod(a: ref RootObj) {.base, raises: [IoError].} =
  38. quit "override"
  39. method doMethod(a: ref Test) =
  40. echo "hello"
  41. if a == nil:
  42. raise newException(IoError, "arg")
  43. proc doProc(a: ref Test) =
  44. echo "hello"
  45. proc newTest(): ref Test =
  46. new(result)
  47. var s:ref Test = newTest()
  48. #doesn't work
  49. for z in 1..4:
  50. s.doMethod()
  51. break