I have recently stumbled on an error after a type declaration. I have declared
the following object type and tried to instantiate a variable with this object:
type
myObject = object
tab: seq
var a1 = myObject(tab: @[1, 2, 3])
Run
but it returns an `Error: invalid type: 'T' in this context: 'myObject' for
var`. Indeed, seq is a collection of objects and need to know at compile-time
the size of objects. So the following would be good alternatives: Either we
specify the type of the elements of the sequence,
type
myListOfInt = object
tab: seq[int]
var a1 = myListOfInt(tab: @[1, 2, 3])
Run
or we make the type generic:
type
myGenericObject[T] = object
tab: seq[T]
var a1 = myGenericObject[int](tab: @[1, 2, 3])
Run
I am not getting the reason why the type `myObject` compiles, even though we
can not instantiate any object of this type.
Is there any reason to not raise an error, e.g. is this a design decision ?
A quick look in nim-lang/nim issues list (with the filter: is:issue is:open
type generics) did not return an issue that looks similar to my problem
described above. I am hesitant to open a RFC or an issue, I want to check first
if you know any related issue, or if you do not think that this is an issue.