Created
February 14, 2015 16:29
-
-
Save gmpreussner/17d4c61216cb744e270e to your computer and use it in GitHub Desktop.
Generic param in nested type resolve issue
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
# 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 | |
# Output: | |
# a23: 2x3 | |
# a32: 3x2 | |
# Looking good. Let's add a proc: | |
proc transpose*[R, C, T](m: RectArray[R, C, T]): RectArray[C, R, T] = | |
echo "transpose" | |
var t32 = a23.transpose | |
echo "t32: ", t32.R, "x", t32.C | |
# Output: | |
# t32: 3x2 | |
# Everything is still OK. Now let's use the rectangular array inside another | |
# generic type: | |
type | |
Matrix*[R, C: static[int], T] = object | |
a*: RectArray[R, C, T] | |
var m23: Matrix[2, 3, int] | |
var m32: Matrix[3, 2, int] | |
echo "m23: ", m23.R, "x", m23.C, " (", m23.a.R, "x", m23.a.C, ")" | |
echo "m32: ", m32.R, "x", m32.C, " (", m32.a.R, "x", m32.a.C, ")" | |
# Output: | |
# m23: 2x3 (2x3) | |
# m32: 3x2 (3x2) | |
# Everything is still as expected. Now let's add the following proc: | |
proc transpose*[R, C, T](m: Matrix[R, C, T]): Matrix[C, R, T] = | |
echo "transpose" | |
var x23: Matrix[2, 3, int] | |
var x32 = x23.transpose | |
echo "x23: ", x23.R, "x", x23.C, " (", x23.a.R, "x", x23.a.C, ")" | |
echo "x32: ", x32.R, "x", x32.C, " (", x32.a.R, "x", x32.a.C, ")" | |
# Output: | |
# x23: 2x3 (2x3) | |
# x32: 3x2 (2x3) <--- this is incorrect. R and C do not match! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above problems seem to be specific to static[T] parameters. For reference, the following example works correctly: