tmethod_various.nim 1.4 KB

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