Thank you all very much for the welcome and for all the help! I know see where
the problem is, and I hope that, in the future, an easier syntax would work.
In any case, I went with the following solution, using a generic type at the
type level and specializing only in the procs' interface:
type
AbstractType = int or string
#Fully generic type, but constrained in the procs interface.
TestType*[T, Y] = 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
What do you think of this solution? The only problem here is that, to construct
a
TestType
Run
one should always use the
newTestType
Run
proc in order to have the correct
AbstractType
Run
behaviour.