texplicitgeneric1.nim 941 B

12345678910111213141516171819202122232425262728293031323334353637
  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, #with `==`(a, b: TKey): bool
  9. # hash(a: TKey): int,
  10. TValue] = ref TDict[TKey, TValue]
  11. proc newDict*[TKey, TValue](): PDict[TKey, TValue] =
  12. new(result)
  13. result.data = @[]
  14. proc add*[TKey, TValue](d: PDict[TKey, TValue], k: TKey, v: TValue) =
  15. d.data.add((k, v))
  16. iterator items*[Tkey, TValue](d: PDict[TKey, TValue]): tuple[k: TKey,
  17. v: TValue] =
  18. for k, v in items(d.data): yield (k, v)
  19. var d = newDict[int, string]()
  20. d.add(12, "12")
  21. d.add(13, "13")
  22. for k, v in items(d):
  23. stdout.write("Key: ", $k, " value: ", v)
  24. var c = newDict[char, string]()
  25. c.add('A', "12")
  26. c.add('B', "13")
  27. for k, v in items(c):
  28. stdout.write(" Key: ", $k, " value: ", v)
  29. stdout.write "\n"