typeclassinference.nim 937 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. discard """
  2. errormsg: "type mismatch: got <string> but expected 'ptr'"
  3. line: 20
  4. """
  5. import typetraits
  6. type
  7. Vec[N: static[int]; T] = distinct array[N, T]
  8. var x = Vec([1, 2, 3])
  9. static:
  10. assert x.type.name == "Vec[3, system.int]"
  11. var str1: string = "hello, world!"
  12. var ptr1: ptr = addr(str1)
  13. var str2: string = "hello, world!"
  14. var ptr2: ptr = str2
  15. block: # built in typeclass inference
  16. proc tupleA(): tuple = return (1, 2)
  17. proc tupleB(): tuple = (1f, 2f)
  18. assert typeof(tupleA()) is (int, int)
  19. assert typeof(tupleB()) is (float32, float32)
  20. proc a(val: int or float): tuple =
  21. when typeof(val) is int:
  22. (10, 10)
  23. else:
  24. (30f, 30f)
  25. assert typeof(a(10)) is (int, int)
  26. assert typeof(a(10.0)) is (float32, float32)
  27. proc b(val: int or float): set =
  28. when typeof(val) is int:
  29. {10u8, 3}
  30. else:
  31. {'a', 'b'}
  32. assert typeof(b(10)) is set[uint8]
  33. assert typeof(b(10.0)) is set[char]