tbindtypedesc.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. discard """
  2. output: '''ok'''
  3. """
  4. import typetraits
  5. type
  6. TFoo = object
  7. x, y: int
  8. TBar = tuple
  9. x, y: int
  10. template accept(e) =
  11. static: assert(compiles(e))
  12. template reject(e) =
  13. static: assert(not compiles(e))
  14. proc genericParamRepeated[T: typedesc](a: T, b: T) =
  15. static:
  16. echo a.name, " ", b.name
  17. accept genericParamRepeated(int, int)
  18. accept genericParamRepeated(float, float)
  19. reject genericParamRepeated(string, int)
  20. reject genericParamRepeated(int, float)
  21. proc genericParamOnce[T: typedesc](a, b: T) =
  22. static:
  23. echo a.name, " ", b.name
  24. accept genericParamOnce(int, int)
  25. accept genericParamOnce(TFoo, TFoo)
  26. reject genericParamOnce(string, int)
  27. reject genericParamOnce(TFoo, float)
  28. type
  29. type1 = typedesc
  30. type2 = typedesc
  31. proc typePairs(A, B: type1; C, D: type2) = discard
  32. accept typePairs(int, int, TFoo, TFOO)
  33. accept typePairs(TBAR, TBar, TBAR, TBAR)
  34. accept typePairs(int, int, string, string)
  35. reject typePairs(TBAR, TBar, TBar, TFoo)
  36. reject typePairs(string, int, TBAR, TBAR)
  37. proc typePairs2[T: typedesc, U: typedesc](A, B: T; C, D: U) = discard
  38. accept typePairs2(int, int, TFoo, TFOO)
  39. accept typePairs2(TBAR, TBar, TBAR, TBAR)
  40. accept typePairs2(int, int, string, string)
  41. reject typePairs2(TBAR, TBar, TBar, TFoo)
  42. reject typePairs2(string, int, TBAR, TBAR)
  43. proc dontBind(a: typedesc, b: typedesc) =
  44. static:
  45. echo a.name, " ", b.name
  46. accept dontBind(int, float)
  47. accept dontBind(TFoo, TFoo)
  48. proc dontBind2(a, b: typedesc) = discard
  49. accept dontBind2(int, float)
  50. accept dontBind2(TBar, int)
  51. proc bindArg(T: typedesc, U: typedesc, a, b: T, c, d: U) = discard
  52. accept bindArg(int, string, 10, 20, "test", "nest")
  53. accept bindArg(int, int, 10, 20, 30, 40)
  54. reject bindArg(int, string, 10, "test", "test", "nest")
  55. reject bindArg(int, int, 10, 20, 30, "test")
  56. reject bindArg(int, string, 10.0, 20, "test", "nest")
  57. reject bindArg(int, string, "test", "nest", 10, 20)
  58. echo "ok"
  59. #11058:
  60. template test(S: type, U: type) =
  61. discard
  62. test(int, float)
  63. proc test2(S: type, U: type) =
  64. discard
  65. test2(float, int)