tmultimjs.nim 1.2 KB

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