texplicitgeneric2.nim 812 B

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