mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-06 17:35:13 +00:00
type constraints; tuple lifting
This commit is contained in:
15
tests/accept/compile/tconstraints.nim
Normal file
15
tests/accept/compile/tconstraints.nim
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
|
||||
proc myGenericProc[T: object|tuple|int|ptr|ref|distinct](x: T): string =
|
||||
result = $x
|
||||
|
||||
type
|
||||
TMyObj = tuple[x, y: int]
|
||||
|
||||
var
|
||||
x: TMyObj
|
||||
|
||||
assert myGenericProc(232) == "232"
|
||||
assert myGenericProc(x) == "(x: 0, y: 0)"
|
||||
|
||||
|
||||
19
tests/accept/compile/tgenericmatcher.nim
Normal file
19
tests/accept/compile/tgenericmatcher.nim
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
type
|
||||
TMatcherKind = enum
|
||||
mkTerminal, mkSequence, mkAlternation, mkRepeat
|
||||
TMatcher[T] = object
|
||||
case kind: TMatcherKind
|
||||
of mkTerminal:
|
||||
value: T
|
||||
of mkSequence, mkAlternation:
|
||||
matchers: seq[TMatcher[T]]
|
||||
of mkRepeat:
|
||||
matcher: PMatcher[T]
|
||||
min, max: int
|
||||
PMatcher[T] = ref TMatcher[T]
|
||||
|
||||
var
|
||||
m: PMatcher[int]
|
||||
|
||||
|
||||
24
tests/accept/compile/tgenericrefs.nim
Normal file
24
tests/accept/compile/tgenericrefs.nim
Normal file
@@ -0,0 +1,24 @@
|
||||
# Compiles:
|
||||
|
||||
type
|
||||
TA[T] = object
|
||||
PA[T] = ref TA[T]
|
||||
var a: PA[string]
|
||||
|
||||
# Compiles unless you use var a: PA[string]
|
||||
type
|
||||
PA = ref TA
|
||||
TA[T] = object
|
||||
|
||||
|
||||
# Cannot instanciate:
|
||||
type
|
||||
TA[T] = object
|
||||
a: PA[T]
|
||||
PA[T] = ref TA[T]
|
||||
|
||||
type
|
||||
PA[T] = ref TA[T]
|
||||
TA[T] = object
|
||||
|
||||
|
||||
46
tests/accept/run/tfielditerator.nim
Normal file
46
tests/accept/run/tfielditerator.nim
Normal file
@@ -0,0 +1,46 @@
|
||||
discard """
|
||||
output: '''
|
||||
a char: true
|
||||
a char: false
|
||||
an int: 5
|
||||
an int: 6
|
||||
a string: abc
|
||||
false
|
||||
true
|
||||
true
|
||||
false
|
||||
true
|
||||
a: a
|
||||
b: b
|
||||
x: 5
|
||||
y: 6
|
||||
z: abc
|
||||
'''
|
||||
"""
|
||||
|
||||
type
|
||||
TMyTuple = tuple[a, b: char, x, y: int, z: string]
|
||||
|
||||
proc p(x: char) = echo "a char: ", x <= 'a'
|
||||
proc p(x: int) = echo "an int: ", x
|
||||
proc p(x: string) = echo "a string: ", x
|
||||
|
||||
var x: TMyTuple = ('a', 'b', 5, 6, "abc")
|
||||
var y: TMyTuple = ('A', 'b', 5, 9, "abc")
|
||||
|
||||
for f in fields(x):
|
||||
p f
|
||||
|
||||
for a, b in fields(x, y):
|
||||
echo a == b
|
||||
|
||||
for key, val in fieldPairs(x):
|
||||
echo key, ": ", val
|
||||
|
||||
assert x != y
|
||||
assert x == x
|
||||
assert(not (x < x))
|
||||
assert x <= x
|
||||
assert y < x
|
||||
assert y <= x
|
||||
|
||||
17
tests/accept/run/tkoeniglookup.nim
Normal file
17
tests/accept/run/tkoeniglookup.nim
Normal file
@@ -0,0 +1,17 @@
|
||||
discard """
|
||||
output: '''x: 0 y: 0'''
|
||||
"""
|
||||
|
||||
proc ToString[T]*(x: T): string = return $x
|
||||
|
||||
|
||||
type
|
||||
TMyObj = object
|
||||
x, y: int
|
||||
|
||||
proc `$`*(a: TMyObj): bool =
|
||||
result = "x: " & a.x & " y: " & a.y
|
||||
|
||||
var a: TMyObj
|
||||
echo toString(a)
|
||||
|
||||
18
tests/reject/tconstraints.nim
Normal file
18
tests/reject/tconstraints.nim
Normal file
@@ -0,0 +1,18 @@
|
||||
discard """
|
||||
line: 15
|
||||
errormsg: "type mismatch: got (int)"
|
||||
"""
|
||||
|
||||
proc myGenericProc[T: object|tuple|ptr|ref|distinct](x: T): string =
|
||||
result = $x
|
||||
|
||||
type
|
||||
TMyObj = tuple[x, y: int]
|
||||
|
||||
var
|
||||
x: TMyObj
|
||||
|
||||
assert myGenericProc(232) == "232"
|
||||
assert myGenericProc(x) == "(x: 0, y: 0)"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user