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])
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])
or we make the type generic:
type
myGenericObject[T] = object
tab: seq[T]
var a1 = myGenericObject[int](tab: @[1, 2, 3])
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.
Well, you may disagree but it does. I can imagine a header file with only complex types depending on top of others, with no instantiation as an API only. Sure this should be detected by examples providing instantiations of the types.
We can also imagine a user writing a type with a field requiring a type that is not generic at first, and does not detect the change. The raised errors doesn't mention at all the field concerned by the error.
Why do we not raise a warning (not necessarily an error) that a type contains generic types in the fields, and these have not been specified ?
If this a design decision, then may I ask if it has been done in other languages ? If it is a new design, what does it bring ?
I take it all back you're completely correct, and it is a bug.
Replacing seq with any other typeclass gives an error I think you'd appreciate: > Error 'seq' is not a concrete type