This commit is contained in:
Araq 2015-03-16 23:02:03 +01:00
commit bf90b9c833
9 changed files with 107 additions and 97 deletions

View file

@ -0,0 +1,44 @@
discard """
output: '''1
2
3
4
5
6
a
b
t
e
s
t
'''
disabled: "true"
"""
template accept(e: expr) =
static: assert compiles(e)
template reject(e: expr) =
static: assert(not compiles(e))
type
Container[T] = generic C
C.len is Ordinal
items(c) is iterator
for value in C:
type(value) is T
proc takesIntContainer(c: Container[int]) =
for e in c: echo e
takesIntContainer(@[1, 2, 3])
reject takesIntContainer(@["x", "y"])
proc takesContainer(c: Container) =
for e in c: echo e
takesContainer(@[4, 5, 6])
takesContainer(@["a", "b"])
takesContainer "test"
reject takesContainer(10)

View file

@ -0,0 +1,80 @@
discard """
output: '''3
[1, 3]
[2, 1, 2]
'''
disabled: "true"
"""
import macros, strutils
template accept(e: expr) =
static: assert(compiles(e))
template reject(e: expr) =
static: assert(not compiles(e))
proc swizzleIdx(c: char): int =
return case c
of 'x': 0
of 'y': 1
of 'z': 2
of 'w': 3
of 'r': 0
of 'g': 1
of 'b': 2
of 'a': 3
else: 0
proc isSwizzle(s: string): bool {.compileTime.} =
template trySet(name, set) =
block search:
for c in s:
if c notin set:
break search
return true
trySet coords, {'x', 'y', 'z', 'w'}
trySet colors, {'r', 'g', 'b', 'a'}
return false
type
StringIsSwizzle = generic value
value.isSwizzle
SwizzleStr = static[string] and StringIsSwizzle
proc foo(x: SwizzleStr) =
echo "sw"
#foo("xx")
reject foo("xe")
type
Vec[N: static[int]; T] = array[N, T]
when false:
proc card(x: Vec): int = x.N
proc `$`(x: Vec): string = x.repr.strip
macro `.`(x: Vec, swizzle: SwizzleStr): expr =
var
cardinality = swizzle.len
values = newNimNode(nnkBracket)
v = genSym()
for c in swizzle:
values.add newNimNode(nnkBracketExpr).add(
v, c.swizzleIdx.newIntLitNode)
return quote do:
let `v` = `x`
Vec[`cardinality`, `v`.T](`values`)
var z = Vec([1, 2, 3])
#echo z.card
#echo z.xz
#echo z.yxy

View file

@ -0,0 +1,68 @@
discard """
output: '''Sortable
Sortable
Container
true
true
false
false
false
'''
"""
import typetraits
type
TObj = object
x: int
Sortable = generic x, y
(x < y) is bool
ObjectContainer = generic C
C.len is Ordinal
for v in items(C):
v.type is tuple|object
proc foo(c: ObjectContainer) =
echo "Container"
proc foo(x: Sortable) =
echo "Sortable"
foo 10
foo "test"
foo(@[TObj(x: 10), TObj(x: 20)])
proc intval(x: int): int = 10
# check real and virtual fields
type
TFoo = generic T
T.x
y(T)
intval T.y
let z = intval(T.y)
proc y(x: TObj): int = 10
proc testFoo(x: TFoo) = discard
testFoo(TObj(x: 10))
type
Matrix[Rows, Cols: static[int]; T] = generic M
M.M == Rows
M.N == Cols
M.T is T
MyMatrix[M, N: static[int]; T] = object
data: array[M*N, T]
var x: MyMatrix[3, 3, int]
echo x is Matrix
echo x is Matrix[3, 3, int]
echo x is Matrix[3, 3, float]
echo x is Matrix[4, 3, int]
echo x is Matrix[3, 4, int]

View file

@ -0,0 +1,24 @@
type
hasFieldX = generic z
z.x is int
obj_x = object
x: int
ref_obj_x = ref object
x: int
ref_to_obj_x = ref obj_x
p_o_x = ptr obj_x
v_o_x = var obj_x
template check(x) =
static: assert(x)
check obj_x is hasFieldX
check ref_obj_x is hasFieldX
check ref_to_obj_x is hasFieldX
check p_o_x is hasFieldX
check v_o_x is hasFieldX