Skip to content

Instantly share code, notes, and snippets.

@gmpreussner
gmpreussner / gist:17d4c61216cb744e270e
Created February 14, 2015 16:29
Generic param in nested type resolve issue
# Suppose we have the following type for a rectangular array:
type
RectArray*[R, C: static[int], T] = distinct array[R * C, T]
var a23: RectArray[2, 3, int]
var a32: RectArray[3, 2, int]
echo "a23: ", a23.R, "x", a23.C
echo "a32: ", a32.R, "x", a32.C
@gmpreussner
gmpreussner / gist:34b6bf829f8a2e46b2bb
Created January 25, 2015 06:53
Generic tuple types
# compiles
type Foo = tuple[x, y: float]
proc foo(a, b: Foo): bool = (a.x == b.x) and (a.y == b.y)
var f = (1.0, 2.0)
check(foo(f, f))
# does not compile
type Bar[T] = tuple[x, y: T]
proc bar(a, b: Bar): bool = (a.x == b.x) and (a.y == b.y) # Error: undeclared identifier: 'x'
var b = (1.0, 2.0)
# this works ##########################
type
Foo[N: static[int], T] = object
elements: array[0..(N - 1), T]
var f2 = Foo[2, float](elements: [1.0, 2.0])
var f3 = Foo[3, float](elements: [1.0, 2.0, 3.0])