-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic parameters now can constrain statics in type definitions (#19362
) * Parameters now can constrain static in type definitions resolved regression with generic procedures * Update compiler/sigmatch.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
- Loading branch information
Showing
2 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
discard """ | ||
cmd: "nim check --hints:off --warnings:off $file" | ||
action: "reject" | ||
nimout:''' | ||
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)] | ||
got: <typedesc[int], int literal(10)> | ||
but expected: <T: float or string, Y> | ||
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)] | ||
got: <typedesc[int], int literal(10)> | ||
but expected: <T: float or string, Y> | ||
tstatic_constrained.nim(41, 29) Error: object constructor needs an object type [proxy] | ||
tstatic_constrained.nim(41, 29) 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)] | ||
got: <typedesc[byte], uint8> | ||
but expected: <T: float or string, Y> | ||
tstatic_constrained.nim(42, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)] | ||
got: <typedesc[byte], uint8> | ||
but expected: <T: float or string, Y> | ||
tstatic_constrained.nim(42, 32) Error: object constructor needs an object type [proxy] | ||
tstatic_constrained.nim(42, 32) Error: expression '' has no type (or is ambiguous) | ||
''' | ||
""" | ||
|
||
type | ||
MyType[T; X: static T] = object | ||
data: T | ||
MyOtherType[T: float or string, Y: static T] = object | ||
|
||
func f[T,X](a: MyType[T,X]): MyType[T,X] = | ||
when T is string: | ||
MyType[T,X](data: a.data & X) | ||
else: | ||
MyType[T,X](data: a.data + X) | ||
|
||
discard MyType[int, 2](data: 1) | ||
discard MyType[string, "Helelello"](data: "Hmmm") | ||
discard MyType[int, 2](data: 1).f() | ||
discard MyType[string, "Helelello"](data: "Hmmm").f() | ||
discard MyOtherType[float, 1.3]() | ||
discard MyOtherType[string, "Hello"]() | ||
discard MyOtherType[int, 10]() | ||
discard MyOtherType[byte, 10u8]() |