tgenericdefaultparam.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. block: # issue #16700
  2. type MyObject[T] = object
  3. x: T
  4. proc initMyObject[T](value = T.default): MyObject[T] =
  5. MyObject[T](x: value)
  6. var obj = initMyObject[int]()
  7. block: # issue #20916
  8. type
  9. SomeX = object
  10. v: int
  11. var val = 0
  12. proc f(_: type int, x: SomeX, v = x.v) =
  13. doAssert v == 42
  14. val = v
  15. proc a(): proc() =
  16. let v = SomeX(v: 42)
  17. var tmp = proc() =
  18. int.f(v)
  19. tmp
  20. a()()
  21. doAssert val == 42
  22. import std/typetraits
  23. block: # issue #24099, original example
  24. type
  25. ColorRGBU = distinct array[3, uint8] ## RGB range 0..255
  26. ColorRGBAU = distinct array[4, uint8] ## RGB range 0..255
  27. ColorRGBUAny = ColorRGBU | ColorRGBAU
  28. template componentType(t: typedesc[ColorRGBUAny]): typedesc =
  29. ## Returns component type of a given color type.
  30. arrayType distinctBase t
  31. func `~=`[T: ColorRGBUAny](a, b: T, e = componentType(T)(1.0e-11)): bool =
  32. ## Compares colors with given accuracy.
  33. abs(a[0] - b[0]) < e and abs(a[1] - b[1]) < e and abs(a[2] - b[2]) < e
  34. block: # issue #24099, modified to actually work
  35. type
  36. ColorRGBU = distinct array[3, uint8] ## RGB range 0..255
  37. ColorRGBAU = distinct array[4, uint8] ## RGB range 0..255
  38. ColorRGBUAny = ColorRGBU | ColorRGBAU
  39. template arrayType[I, T](t: typedesc[array[I, T]]): typedesc =
  40. T
  41. template `[]`(a: ColorRGBUAny, i: untyped): untyped = distinctBase(a)[i]
  42. proc abs(a: uint8): uint8 = a
  43. template componentType(t: typedesc[ColorRGBUAny]): typedesc =
  44. ## Returns component type of a given color type.
  45. arrayType distinctBase t
  46. func `~=`[T: ColorRGBUAny](a, b: T, e = componentType(T)(1.0e-11)): bool =
  47. ## Compares colors with given accuracy.
  48. abs(a[0] - b[0]) <= e and abs(a[1] - b[1]) <= e and abs(a[2] - b[2]) <= e
  49. doAssert ColorRGBU([1.uint8, 1, 1]) ~= ColorRGBU([1.uint8, 1, 1])
  50. block: # issue #24099, modified to work but using float32
  51. type
  52. ColorRGBU = distinct array[3, float32] ## RGB range 0..255
  53. ColorRGBAU = distinct array[4, float32] ## RGB range 0..255
  54. ColorRGBUAny = ColorRGBU | ColorRGBAU
  55. template arrayType[I, T](t: typedesc[array[I, T]]): typedesc =
  56. T
  57. template `[]`(a: ColorRGBUAny, i: untyped): untyped = distinctBase(a)[i]
  58. template componentType(t: typedesc[ColorRGBUAny]): typedesc =
  59. ## Returns component type of a given color type.
  60. arrayType distinctBase t
  61. func `~=`[T: ColorRGBUAny](a, b: T, e = componentType(T)(1.0e-11)): bool =
  62. ## Compares colors with given accuracy.
  63. abs(a[0] - b[0]) < e and abs(a[1] - b[1]) < e and abs(a[2] - b[2]) < e
  64. doAssert ColorRGBU([1.float32, 1, 1]) ~= ColorRGBU([1.float32, 1, 1])
  65. block: # issue #13270
  66. type
  67. A = object
  68. B = object
  69. proc f(a: A) = discard
  70. proc g[T](value: T, cb: (proc(a: T)) = f) =
  71. cb value
  72. g A()
  73. # This should fail because there is no f(a: B) overload available
  74. doAssert not compiles(g B())
  75. block: # issue #24121
  76. type
  77. Foo = distinct int
  78. Bar = distinct int
  79. FooBar = Foo | Bar
  80. proc foo[T: distinct](x: T): string = "a"
  81. proc foo(x: Foo): string = "b"
  82. proc foo(x: Bar): string = "c"
  83. proc bar(x: FooBar, y = foo(x)): string = y
  84. doAssert bar(Foo(123)) == "b"
  85. doAssert bar(Bar(123)) == "c"
  86. proc baz[T: FooBar](x: T, y = foo(x)): string = y
  87. doAssert baz(Foo(123)) == "b"
  88. doAssert baz(Bar(123)) == "c"