tmethod_various.nim 1.3 KB

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