proc foo[T]() = discard
proc foo[T, V]() = discard
Error: redefinition of 'foo'; previous declaration here: test.nim(1, 6)
Why??? I would assume that a generic proc with no argument just collapse to a plain proc with no argument -- and so we have redefinition.
At least I do not know which sense generics can make when there are no proc arguments.
At least I do not know which sense generics can make when there are no proc arguments.
type Foo[T] = object
proc createFoo[T](): Foo[T] =
...
This doesn't work too:
type Foo[T] = object
type Foo[T, V] = object
There's no overloading for generic parameters.
You still can achieve what you want, by passing types as normal arguments — they still exist at compile time only, for procedure instantiation, that is they are really the same generic parameters.
type Foo[T,V] = object
x: T
y: V
proc createFoo(T: typedesc, V: typedesc = void): Foo[T,V] =
discard
var foo1 = createFoo(string,void)
var foo2 = createFoo(int,float)
echo foo1, "\n", foo2
void means the absence of a value, that is in the first case the returned object has just one field.