tmethod_various.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. discard """
  2. matrix: "--mm:arc; --mm:refc"
  3. output: '''
  4. HELLO WORLD!
  5. '''
  6. """
  7. type
  8. TNode* {.inheritable.} = object
  9. PNode* = ref TNode
  10. PNodeFoo* = ref object of TNode
  11. TSomethingElse = object
  12. PSomethingElse = ref TSomethingElse
  13. method foo(a: PNode, b: PSomethingElse) {.base.} = discard
  14. method foo(a: PNodeFoo, b: PSomethingElse) = discard
  15. # tmproto
  16. type
  17. Obj1 {.inheritable.} = ref object
  18. Obj2 = ref object of Obj1
  19. method beta(x: Obj1): int {.base.}
  20. proc delta(x: Obj2): int =
  21. beta(x)
  22. method beta(x: Obj2): int
  23. proc alpha(x: Obj1): int =
  24. beta(x)
  25. method beta(x: Obj1): int = 1
  26. method beta(x: Obj2): int = 2
  27. proc gamma(x: Obj1): int =
  28. beta(x)
  29. doAssert alpha(Obj1()) == 1
  30. doAssert gamma(Obj1()) == 1
  31. doAssert alpha(Obj2()) == 2
  32. doAssert gamma(Obj2()) == 2
  33. doAssert delta(Obj2()) == 2
  34. # tsimmeth
  35. import strutils
  36. var x = "hello world!".toLowerAscii.toUpperAscii
  37. x.echo()
  38. # trecmeth
  39. # Note: We only compile this to verify that code generation
  40. # for recursive methods works, no code is being executed
  41. type Obj = ref object of RootObj
  42. # Mutual recursion
  43. method alpha(x: Obj) {.base.}
  44. method beta(x: Obj) {.base.}
  45. method alpha(x: Obj) =
  46. beta(x)
  47. method beta(x: Obj) =
  48. alpha(x)
  49. # Simple recursion
  50. method gamma(x: Obj) {.base.} =
  51. gamma(x)