texplicitgeneric2.nim 801 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. output: "Key: 12 value: 12Key: 13 value: 13 Key: A value: 12 Key: B value: 13"
  3. """
  4. # test explicit type instantiation
  5. type
  6. TDict*[TKey, TValue] = object
  7. data: seq[tuple[k: TKey, v: TValue]]
  8. PDict*[TKey, TValue] = ref TDict[TKey, TValue]
  9. proc newDict*[TKey, TValue](): PDict[TKey, TValue] =
  10. new(result)
  11. result.data = @[]
  12. proc add*(d: PDict, k: d.TKey, v: d.TValue) =
  13. d.data.add((k, v))
  14. iterator items*(d: PDict): tuple[k: d.TKey, v: d.TValue] =
  15. for k, v in items(d.data): yield (k, v)
  16. var d = newDict[int, string]()
  17. d.add(12, "12")
  18. d.add(13, "13")
  19. for k, v in items(d):
  20. stdout.write("Key: ", $k, " value: ", v)
  21. var c = newDict[char, string]()
  22. c.add('A', "12")
  23. c.add('B', "13")
  24. for k, v in items(c):
  25. stdout.write(" Key: ", $k, " value: ", v)
  26. stdout.write "\n"