I am trying to implement a dual number type in NIM, but struggling with with,
for me, obscure error messages. Most likely I am missing something trivial, but
after having poured in more than 2 hours in trying stuff and researching I
still could not figure it out. Here's the minimum not working example:
type
Dual*[N: static[int], T: SomeFloat | Dual] = object
real*: T
dual*: array[N, T]
proc dual*[N: static[int]; T: SomeFloat | Dual](real: T, dual: array[N,
T]): Dual[N, T] =
result.real = real
result.dual = dual
# scalar multiplication
proc `*`*[N: static[int]; T: SomeFloat | Dual](x: SomeFloat; y: Dual[N,
T]): Dual[N, T] =
result.real = x * y.real
for i in 0..N-1:
result.dual[i] = x * y.dual[i]
Run
Testing this gives different errors which I put in the comments above the
relevant lines of code.
# Test
#let a = dual[2, Dual[2, float]](
# dual[2, float](1.0, [0.0, 0.0]),
# [dual[2, float](0.0, [0.0, 0.0]), dual[2, float](1.0, [0.0, 0.0])]
#)
let a = dual(3.0, [1.0, 0.0])
echo a # works as expected
# Error: cannot instantiate 'Dual[N, T: float or float32 or float64 or
Dual]' inside of type definition: '*'; Maybe generic arguments are missing?
echo 183.0*a
# Error: cannot instantiate: '*[2, float]'
echo `*`[2, float](183.0, a)
Run
Thanks in advance, ~whiterock