I would like to write a polymorphic function that does slightly different things based on whether the parameters are of type A or static[A]. Ideally I would use the same name for the function in the static and non static case, but incorporate more compile-time imformation when available statically.
An example would be the function to generate the identity matrix of dimension N. One could call
let m = eye(N)
and - based on whether N is known at compile time or not (for instance N may be a literal) - get a slightly different representation of the matrix. In the static case, the matrix will incorporate the dimension in its type, allowing more compile time checks, while in the other case all dimension checks will be postponed to runtime.
This is because I already support matrices of known dimension in my linear algebra library, and I would like to support matrices of unknown dimension without changing the API too much - if possible.
Is this kind of dispatch supported? It seems to work sometimes, based on the order of definitions, but I do not know how stable the support for this feature is (it may happen by chance)
template isStatic(a: typed): bool =
compiles(proc() =
const v = a)
proc foo(a: int | static[int]) =
when isStatic(a):
echo "static: ", a
else:
echo "not static: ", a
foo(5)
var b = 6
foo(b)