I am trying to write a function whose signature depends on a int parameter which is known, but not fixed, at compile time.
As long as the dependence is in the return type, I can use a static[int] as a type parameter, and everything works fine, for instance
type Foo[n: static[int]] = array[n, int]
const
  N = 10
  M = 20
proc `$`(f: Foo): string = $(@f)
proc makeFoo(n: static[int]): Foo[n] =
  for i in 0 .. < n:
    result[i] = i
when isMainModule:
  echo makeFoo(N)
  echo makeFoo(M)The problem arises when I need to have dependence on the static[int] in the function parameters. For instance, I can define
proc findMin(n: static[int], f: Foo[n]): int =
  result = high(int)
  for x in f:
    if result > x:
      result = x
let f = makeFoo(N)
echo findMin(N, f)This does not compile saying mini.nim(12, 37) Error: type expected. The error points at the n in Foo[n], which is in fact a static[int].
If I interpret correctly the section on static[T] in the manual, the above should work, but it seems I misunderstood something.
The same error appears even if I declare findMin like
proc findMin[n: static[int]](f: Foo[n]): int = ...or even
template findMin(n: static[int], f: Foo[n]): int = ...What is the right way to define findMin, so that I get different versions for each constant value that is used at compile time?
It works if you make it generic over n, like this:
type Foo[N: static[int]] = array[N, int]
const
  N = 10
  M = 20
proc `$`(f: Foo): string = $(@f)
proc makeFoo(n: static[int]): Foo[n] =
  for i in 0 .. < n:
    result[i] = i
proc findMin[n](f: Foo[n]): int =
  result = high(int)
  for x in f:
    if result > x:
      result = x
let f = makeFoo(10)
echo findMin(f)
echo findMin[N](f)and it would fail. It seems that it works only if I let the compiler infer the static number instead of passing it explicitly.
Anyway, thank you, for now this works fine for me