Hello everyone! I am a new Nim user (and, I have to say, I am pretty excited
about this language!) coming from Julia. I was tesing some of Nim's
capabilities when coming to generic types and procs, when I ran into this code
not compiling:
type
AbstractType = int or string
TestType*[T : AbstractType, Y : AbstractType] = object
a : T
b : Y
proc newTestType[T : AbstractType, Y : AbstractType](a : T, b : Y) :
TestType[T, Y] =
return TestType[T, Y](a : a, b : b)
proc print_test_type(t : TestType) =
echo t.a
echo t.b
let
t1 = newTestType(1, 2)
t2 = newTestType("string", "here")
t3 = newTestType(10, "mixed")
echo typedesc(t1)
print_test_type(t1)
echo typedesc(t2)
print_test_type(t2)
echo typedesc(t3)
print_test_type(t3)
Run
The compiler error is this:
/home/francesco/Sources/nim/GenericTypes.nim(44, 21) template/generic
instantiation of `newTestType` from here
/home/francesco/Sources/nim/GenericTypes.nim(35, 20) Error: cannot
instantiate TestType
got: <type int, type string>
but expected: <T: AbstractType, Y: AbstractType>
Run
On the other hand, if I substitute
TestType*[T : AbstractType, Y : AbstractType]
Run
with
TestType*[T : AbstractType, Y : int or string]
Run
, the code correctly compiles and executes, resulting in the expected result:
[system.int, system.int]
1
2
TestType[system.string, system.string]
string
here
TestType[system.int, system.string]
10
mixed
Run
Does anyone know why this is the case?