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