tconcepts_overload_precedence.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. discard """
  2. output: '''x as ParameterizedType[T]
  3. x as ParameterizedType[T]
  4. x as ParameterizedType[T]
  5. x as ParameterizedType
  6. x as ParameterizedType
  7. x as CustomTypeClass'''
  8. """
  9. type ParameterizedType[T] = object
  10. type CustomTypeClass = concept c
  11. true
  12. # 3 competing procs
  13. proc a[T](x: ParameterizedType[T]) =
  14. echo "x as ParameterizedType[T]"
  15. proc a(x: ParameterizedType) =
  16. echo "x as ParameterizedType"
  17. proc a(x: CustomTypeClass) =
  18. echo "x as CustomTypeClass"
  19. # the same procs in different order
  20. proc b(x: ParameterizedType) =
  21. echo "x as ParameterizedType"
  22. proc b(x: CustomTypeClass) =
  23. echo "x as CustomTypeClass"
  24. proc b[T](x: ParameterizedType[T]) =
  25. echo "x as ParameterizedType[T]"
  26. # and yet another order
  27. proc c(x: CustomTypeClass) =
  28. echo "x as CustomTypeClass"
  29. proc c(x: ParameterizedType) =
  30. echo "x as ParameterizedType"
  31. proc c[T](x: ParameterizedType[T]) =
  32. echo "x as ParameterizedType[T]"
  33. # remove the most specific one
  34. proc d(x: ParameterizedType) =
  35. echo "x as ParameterizedType"
  36. proc d(x: CustomTypeClass) =
  37. echo "x as CustomTypeClass"
  38. # then shuffle the order again
  39. proc e(x: CustomTypeClass) =
  40. echo "x as CustomTypeClass"
  41. proc e(x: ParameterizedType) =
  42. echo "x as ParameterizedType"
  43. # the least specific one is a match
  44. proc f(x: CustomTypeClass) =
  45. echo "x as CustomTypeClass"
  46. a(ParameterizedType[int]())
  47. b(ParameterizedType[int]())
  48. c(ParameterizedType[int]())
  49. d(ParameterizedType[int]())
  50. e(ParameterizedType[int]())
  51. f(ParameterizedType[int]())