trettypeinference.nim 703 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. msg: "instantiated for string\ninstantiated for int\ninstantiated for bool"
  3. output: "int\nseq[string]\nA\nB\n100\ntrue"
  4. """
  5. import typetraits
  6. proc plus(a, b: auto): auto = a + b
  7. proc makePair(a, b: auto): auto = (first: a, second: b)
  8. proc `+`(a, b: string): seq[string] = @[a, b]
  9. var i = plus(10, 20)
  10. var s = plus("A", "B")
  11. var p = makePair("key", 100)
  12. static: assert p[0].type is string
  13. echo i.type.name
  14. echo s.type.name
  15. proc inst(a: auto): auto =
  16. static: echo "instantiated for ", a.type.name
  17. result = a
  18. echo inst("A")
  19. echo inst("B")
  20. echo inst(100)
  21. echo inst(true)
  22. # XXX: [string, tyGenericParam] is cached instead of [string, string]
  23. # echo inst[string, string]("C")