Hello,
I do not understand why the template in the following code fails and its error message: > template/generic instantiation of matrix22 from here > Error: type expected, but got: 2
I am trying to create some utils functions for creating a matrix.
Could someone help me ?
type
  Matrix*[H,W: static int, T] = object
    data: array[H, array[W,T]]
# Identity or 0
func matrix*[H,W: static int,T](): Matrix[H,W,T] {.inline.} =
  when H == W:
    for i in 0..<H:
      result.data[i][i] = T(1)
  else:
    discard
# Set a value in the whole matrix
# -> If remove this function or put it after the template block, it compiles
func matrix*[H,W: static int,T](v: T): Matrix[H,W,T] {.inline.} =
  for y in 0..<H:
    for x in 0..<W:
      result.data[y][x] = v
template generateMatrixConstructors(H,W: static int) =
  type
    `Matrix H W`*[T] {.inject.} = Matrix[H,W,T]
  
  func `matrix H W`*[T](): Matrix[H,W,T] {.inject, inline, noinit.} =
    matrix[H,W,T]()
# Error: type expected, but got 2
generateMatrixConstructors(2, 2)
when isMainModule:
  var m1 = matrix22[float]()
  echo m1
I wanted to have generic matrix type and constructors like matrix[2,2,float]() and defined-size constructors like matrix22[float](). Anyway I found a solution to my problem: it is not about templates or generics indeed, the static parameters (H,W static int that define the size of the matrix) should be between the proc/func parenthesis like this:
func matrix*[T](H,W: static int): Matrix[H,W,T] {.inline.} = ...
var m = matrix[float](2,2) # OK
 instead of:
func matrix*[H,W: static int,T](v: T): Matrix[H,W,T] {.inline.} =
var m = matrix[2,2,float]() # Not OK
 I still do not know the reason why the last approach does not work thoughThis doesn't work either:
type
  Matrix*[H, W: static int, T] = object
    data: array[H, array[W,T]]
func matrix*[H, W: static int, T](): Matrix[H, W, T] {.inline.} =
  when H == W:
    for i in 0..<H:
      result.data[i][i] = T(1)
  else:
    discard
func matrix*[H, W: static int, T](v: T): Matrix[H, W, T] {.inline.} =
  for y in 0..<H:
    for x in 0..<W:
      result.data[y][x] = v
type Matrix22*[T] = Matrix[2,2,T]
func matrix22*[T](): Matrix22[T] {.inline, noinit.} =
  matrix[2,2,T]()
when isMainModule:
  var m1 = matrix22[float]()
  echo m1
so it's a static generics issue when combined with overloading, not related to templates.