tstatic_constrained.nim 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. discard """
  2. cmd: "nim check --hints:off --warnings:off $file"
  3. action: "reject"
  4. nimout:'''
  5. tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
  6. got: <typedesc[int], int literal(10)>
  7. but expected: <T: float or string, Y>
  8. tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
  9. got: <typedesc[int], int literal(10)>
  10. but expected: <T: float or string, Y>
  11. tstatic_constrained.nim(44, 31) Error: object constructor needs an object type [error]
  12. tstatic_constrained.nim(44, 31) Error: expression '' has no type (or is ambiguous)
  13. tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
  14. got: <typedesc[byte], uint8>
  15. but expected: <T: float or string, Y>
  16. tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
  17. got: <typedesc[byte], uint8>
  18. but expected: <T: float or string, Y>
  19. tstatic_constrained.nim(45, 34) Error: object constructor needs an object type [error]
  20. tstatic_constrained.nim(45, 34) Error: expression '' has no type (or is ambiguous)
  21. tstatic_constrained.nim(77, 14) Error: cannot instantiate MyType [type declared in tstatic_constrained.nim(71, 5)]
  22. got: <typedesc[float], float64>
  23. but expected: <T: MyConstraint, Y>
  24. '''
  25. """
  26. block:
  27. type
  28. MyType[T; X: static T] = object
  29. data: T
  30. MyOtherType[T: float or string, Y: static T] = object
  31. func f[T,X](a: MyType[T,X]): MyType[T,X] =
  32. when T is string:
  33. MyType[T,X](data: a.data & X)
  34. else:
  35. MyType[T,X](data: a.data + X)
  36. discard MyType[int, 2](data: 1)
  37. discard MyType[string, "Helelello"](data: "Hmmm")
  38. discard MyType[int, 2](data: 1).f()
  39. discard MyType[string, "Helelello"](data: "Hmmm").f()
  40. discard MyOtherType[float, 1.3]()
  41. discard MyOtherType[string, "Hello"]()
  42. discard MyOtherType[int, 10]()
  43. discard MyOtherType[byte, 10u8]()
  44. block:
  45. type
  46. Moduloable = concept m, type M
  47. m mod m is M
  48. Addable = concept a, type A
  49. a + a is A
  50. Modulo[T: Moduloable; Mod: static T] = distinct T
  51. ModuloAdd[T: Moduloable or Addable; Mod: static T] = distinct T
  52. ModuAddable = Addable or Moduloable
  53. ModdAddClass[T: ModuAddable; Mod: static T] = distinct T
  54. proc toMod[T](val: T, modVal: static T): Modulo[T, modVal] =
  55. mixin `mod`
  56. Modulo[T, modVal](val mod modVal)
  57. var
  58. a = 3231.toMod(10)
  59. b = 5483.toMod(10)
  60. discard ModuloAdd[int, 3](0)
  61. discard ModdAddClass[int, 3](0)
  62. block:
  63. type
  64. MyConstraint = int or string
  65. MyOtherConstraint[T] = object
  66. MyType[T: MyConstraint; Y: static T] = object
  67. MyOtherType[T: MyOtherConstraint; Y: static T] = object
  68. var
  69. a: MyType[int, 10]
  70. b: MyType[string, "hello"]
  71. c: MyType[float, 10d]
  72. d: MyOtherType[MyOtherConstraint[float],MyOtherConstraint[float]()]
  73. e: MyOtherType[MyOtherConstraint[int], MyOtherConstraint[int]()]