Skip to content

Instantly share code, notes, and snippets.

@gmpreussner
Last active February 27, 2016 18:32
Show Gist options
  • Save gmpreussner/5ed8d0e4b921564f7e1d to your computer and use it in GitHub Desktop.
Save gmpreussner/5ed8d0e4b921564f7e1d to your computer and use it in GitHub Desktop.
type IntArray[T; N: static[int]] = array[N, T]
proc ones[T](N: static[int]): IntArray[T, N] =
for i in 0 .. < N:
result[i] = 1
echo @[ones[int](5)]
@gmpreussner
Copy link
Author

tstatic_ones_generic.nim(7, 12) Error: cannot instantiate: ones[int]; got 1 type(s) but expected 2

@gmpreussner
Copy link
Author

type inference works...

type IntArray[T; N: static[int]] = array[N, T]

proc `$`(a: IntArray): string = $(@(a))

proc defaults[T](N: static[int], default: T): IntArray[T, N] =
  for i in 0 .. < N:
    result[i] = 1

echo defaults(5, 1.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment