trecursivegenerics.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. block: # Replicates #18728
  2. type
  3. FlipFlop[A, B] = ref object
  4. val: A
  5. next: FlipFlop[B, A]
  6. Trinary[A, B, C] = ref object
  7. next: Trinary[B, C, A]
  8. assert typeof(FlipFlop[int, string]().next) is FlipFlop[string, int]
  9. assert typeof(FlipFlop[string, int]().next) is FlipFlop[int, string]
  10. assert typeof(Trinary[int, float, string]().next) is Trinary[float, string, int]
  11. assert typeof(Trinary[int, float, string]().next.next) is Trinary[string, int, float]
  12. var a = FlipFlop[int, string](val: 100, next: FlipFlop[string, int](val: "Hello"))
  13. assert a.val == 100
  14. assert a.next.val == "Hello"
  15. block: # 18838
  16. type
  17. DoublyLinkedNodeObj[T] = object
  18. value: T
  19. DoublyLinkedNode[T] = ref DoublyLinkedNodeObj[T]
  20. Item[T] = ref object
  21. link: DoublyLinkedNode[Item[T]]
  22. Box = object
  23. proc newDoublyLinkedNode[T](value: T): DoublyLinkedNode[T] =
  24. new(result)
  25. result.value = value
  26. let link = newDoublyLinkedNode(Item[Box]())
  27. import lists
  28. block:
  29. type
  30. Box = object
  31. Item[T] = ref object
  32. link:DoublyLinkedNode[ Item[T] ]
  33. ItemSimple = ref object
  34. link:DoublyLinkedNode[ ItemSimple ]
  35. let link = newDoublyLinkedNode( Item[Box]() )
  36. block: #18897
  37. type
  38. SkipListObj[T] = object
  39. over: SkipList[T]
  40. down: SkipList[T]
  41. value: T
  42. SkipList[T] = ref SkipListObj[T]
  43. GraphObj[N, E; F: static[int]] = object
  44. nodes: SkipList[Node[N, E]]
  45. Graph[N, E; F: static[int]] = ref GraphObj[N, E, F]
  46. Node[N, E] = ref NodeObj[N, E]
  47. NodeObj[N, E] = object
  48. value: N
  49. incoming: SkipList[Edge[N, E]]
  50. outgoing: SkipList[Edge[N, E]]
  51. Edge[N, E] = ref EdgeObj[N, E]
  52. EdgeObj[N, E] = object
  53. value: E
  54. id: int
  55. source: Node[N, E]
  56. target: Node[N, E]
  57. EdgeResult[N, E] = tuple
  58. source: Node[N, E]
  59. edge: Edge[N, E]
  60. target: Node[N, E]
  61. proc newSkipList[T](value: T): SkipList[T] =
  62. static: echo T, " ", typeof(result.value)
  63. result = SkipList[T](value: value)
  64. proc toSkipList[T](values: openArray[T] = @[]): SkipList[T] =
  65. for item in items(values):
  66. if result.isNil:
  67. result = newSkipList(item)
  68. proc newContainer[N, E, F](graph: Graph[N, E, F]; form: typedesc): auto =
  69. result = toSkipList[form]([])
  70. var
  71. result = Graph[int, string, 0]()
  72. result.nodes = result.newContainer(Node[int, string])