I have this line of code:
var ret = DICT(cast[Context](@[]))
DICT takes Context as a param. And a Context is nothing but a seq[(int,int)].
Obviously, an empty sequence can be anything, so I cast it to Context.
Now, the thing is: when I compile it with "c", everything goes smooth; when I compile it with "cpp" (clang++ on macOS), I have the following error:
error: assigning to 'TGenericSeq *' from incompatible type 'tySequence__77YVzYb2AOu2vP0iI0b8Dw *'
T11_ = (tySequence__77YVzYb2AOu2vP0iI0b8Dw*)0;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Any workarounds?
If I'm not missing something, you should call newSeq instead:
var ret = DICT(newSeq[Context]())
But of course, any Nim code you write should either error during nim compilation or compile successfully.
Spot on! (with one slight difference: given that Context=seq[int,int], it has to be:
DICT(newSeq[(int,int)]())
Thanks! ;-)
Ah yes, indeed. If DICT is just a proc that takes a Context, this also works btw:
let ret = DICT(@[])
The compiler can deduce the type of the empty seq itself from the args of proc DICT(c: Context).
Although personally if the use case of an empty seq as the argument comes up more often, I'd set the empty sequence as the default value for the argument proc DICT(s: Context = @[]) instead. And proc newContext(len: int): Context helper is probably also useful to emphasize the intention.