type Quotient[A] = object
a, b: A
template zero(x: int): int = 0
template zero[A](x: Quotient[A]): Quotient[A] = Quotient[A](a: zero(x.a), b: id(x.a))
template id(x: int): int = 1
template id[A](x: Quotient[A]): Quotient[A] = Quotient[A](a: id(x.a), b: id(x.a))
proc quot[A](x: A): Quotient[A] = Quotient[A](a: x, b: id(x))
echo zero(5.quot)
The compilation fails with fail.nim(12, 9) Error: undeclared identifier: 'A'. I think this has something to do with the interaction between templates and generics, but I am not experienced enough to tell what is going wrong
type Quotient[A] = object
a, b: A
template id(x: int): int = 1
proc id[A](x: Quotient[A]): Quotient[A] {.inline.} =
Quotient[A](a: id(x.a), b: id(x.a))
template zero(x: int): int = 0
proc zero[A](x: Quotient[A]): Quotient[A] {.inline.} =
Quotient[A](a: zero(x.a), b: id(x.a))
proc quot[A](x: A): Quotient[A] = Quotient[A](a: x, b: id(x))
echo zero(5.quot)
Andrea, I wrote a rational arithmetic module some time ago
That's (at least) the third one. Rationals should really go into the standard libray before even more people create their own:
https://github.com/Araq/Nim/pull/1840