Fixed concept constraints for static types (#19391)
This commit is contained in:
parent
a93f6e7acc
commit
7bdfeb7819
2 changed files with 71 additions and 25 deletions
|
|
@ -1634,6 +1634,15 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
|
||||||
bindConcreteTypeToUserTypeClass(matched, a)
|
bindConcreteTypeToUserTypeClass(matched, a)
|
||||||
if doBind: put(c, f, matched)
|
if doBind: put(c, f, matched)
|
||||||
result = isGeneric
|
result = isGeneric
|
||||||
|
elif a.len > 0 and a.lastSon == f:
|
||||||
|
# Needed for checking `Y` == `Addable` in the following
|
||||||
|
#[
|
||||||
|
type
|
||||||
|
Addable = concept a, type A
|
||||||
|
a + a is A
|
||||||
|
MyType[T: Addable; Y: static T] = object
|
||||||
|
]#
|
||||||
|
result = isGeneric
|
||||||
else:
|
else:
|
||||||
result = isNone
|
result = isNone
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,41 +2,78 @@ discard """
|
||||||
cmd: "nim check --hints:off --warnings:off $file"
|
cmd: "nim check --hints:off --warnings:off $file"
|
||||||
action: "reject"
|
action: "reject"
|
||||||
nimout:'''
|
nimout:'''
|
||||||
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
|
tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
|
||||||
got: <typedesc[int], int literal(10)>
|
got: <typedesc[int], int literal(10)>
|
||||||
but expected: <T: float or string, Y>
|
but expected: <T: float or string, Y>
|
||||||
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
|
tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
|
||||||
got: <typedesc[int], int literal(10)>
|
got: <typedesc[int], int literal(10)>
|
||||||
but expected: <T: float or string, Y>
|
but expected: <T: float or string, Y>
|
||||||
tstatic_constrained.nim(41, 29) Error: object constructor needs an object type [proxy]
|
tstatic_constrained.nim(44, 31) Error: object constructor needs an object type [proxy]
|
||||||
tstatic_constrained.nim(41, 29) Error: expression '' has no type (or is ambiguous)
|
tstatic_constrained.nim(44, 31) Error: expression '' has no type (or is ambiguous)
|
||||||
tstatic_constrained.nim(42, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
|
tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
|
||||||
got: <typedesc[byte], uint8>
|
got: <typedesc[byte], uint8>
|
||||||
but expected: <T: float or string, Y>
|
but expected: <T: float or string, Y>
|
||||||
tstatic_constrained.nim(42, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
|
tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
|
||||||
got: <typedesc[byte], uint8>
|
got: <typedesc[byte], uint8>
|
||||||
but expected: <T: float or string, Y>
|
but expected: <T: float or string, Y>
|
||||||
tstatic_constrained.nim(42, 32) Error: object constructor needs an object type [proxy]
|
tstatic_constrained.nim(45, 34) Error: object constructor needs an object type [proxy]
|
||||||
tstatic_constrained.nim(42, 32) Error: expression '' has no type (or is ambiguous)
|
tstatic_constrained.nim(45, 34) Error: expression '' has no type (or is ambiguous)
|
||||||
|
tstatic_constrained.nim(77, 14) Error: cannot instantiate MyType [type declared in tstatic_constrained.nim(71, 5)]
|
||||||
|
got: <typedesc[float], float64>
|
||||||
|
but expected: <T: MyConstraint, Y>
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
MyType[T; X: static T] = object
|
||||||
|
data: T
|
||||||
|
MyOtherType[T: float or string, Y: static T] = object
|
||||||
|
|
||||||
type
|
func f[T,X](a: MyType[T,X]): MyType[T,X] =
|
||||||
MyType[T; X: static T] = object
|
when T is string:
|
||||||
data: T
|
MyType[T,X](data: a.data & X)
|
||||||
MyOtherType[T: float or string, Y: static T] = object
|
else:
|
||||||
|
MyType[T,X](data: a.data + X)
|
||||||
|
|
||||||
func f[T,X](a: MyType[T,X]): MyType[T,X] =
|
discard MyType[int, 2](data: 1)
|
||||||
when T is string:
|
discard MyType[string, "Helelello"](data: "Hmmm")
|
||||||
MyType[T,X](data: a.data & X)
|
discard MyType[int, 2](data: 1).f()
|
||||||
else:
|
discard MyType[string, "Helelello"](data: "Hmmm").f()
|
||||||
MyType[T,X](data: a.data + X)
|
discard MyOtherType[float, 1.3]()
|
||||||
|
discard MyOtherType[string, "Hello"]()
|
||||||
|
discard MyOtherType[int, 10]()
|
||||||
|
discard MyOtherType[byte, 10u8]()
|
||||||
|
|
||||||
discard MyType[int, 2](data: 1)
|
block:
|
||||||
discard MyType[string, "Helelello"](data: "Hmmm")
|
type
|
||||||
discard MyType[int, 2](data: 1).f()
|
Moduloable = concept m, type M
|
||||||
discard MyType[string, "Helelello"](data: "Hmmm").f()
|
m mod m is M
|
||||||
discard MyOtherType[float, 1.3]()
|
Addable = concept a, type A
|
||||||
discard MyOtherType[string, "Hello"]()
|
a + a is A
|
||||||
discard MyOtherType[int, 10]()
|
Modulo[T: Moduloable; Mod: static T] = distinct T
|
||||||
discard MyOtherType[byte, 10u8]()
|
ModuloAdd[T: Moduloable or Addable; Mod: static T] = distinct T
|
||||||
|
ModuAddable = Addable or Moduloable
|
||||||
|
ModdAddClass[T: ModuAddable; Mod: static T] = distinct T
|
||||||
|
|
||||||
|
proc toMod[T](val: T, modVal: static T): Modulo[T, modVal] =
|
||||||
|
mixin `mod`
|
||||||
|
Modulo[T, modVal](val mod modVal)
|
||||||
|
var
|
||||||
|
a = 3231.toMod(10)
|
||||||
|
b = 5483.toMod(10)
|
||||||
|
discard ModuloAdd[int, 3](0)
|
||||||
|
discard ModdAddClass[int, 3](0)
|
||||||
|
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
MyConstraint = int or string
|
||||||
|
MyOtherConstraint[T] = object
|
||||||
|
MyType[T: MyConstraint; Y: static T] = object
|
||||||
|
MyOtherType[T: MyOtherConstraint; Y: static T] = object
|
||||||
|
|
||||||
|
var
|
||||||
|
a: MyType[int, 10]
|
||||||
|
b: MyType[string, "hello"]
|
||||||
|
c: MyType[float, 10d]
|
||||||
|
d: MyOtherType[MyOtherConstraint[float],MyOtherConstraint[float]()]
|
||||||
|
e: MyOtherType[MyOtherConstraint[int], MyOtherConstraint[int]()]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue