Compiling the following code
type
Obj*[T] = object
case a*: bool
of false: discard
of true:
b*: seq[T]
var obj = Obj[void](a: false)
Fails in
~/test/a.nim(8, 20) template/generic instantiation of `setLen` from here
~/.choosenim/toolchains/nim-2.0.0/lib/system/seqs_v2.nim(126, 18) template/generic instantiation of `len` from here
~/.choosenim/toolchains/nim-2.0.0/lib/system.nim(711, 13) Error: invalid type: 'void' in this context: 'proc (x: seq[void]): int{.noSideEffect, gcsafe.}' for proc
I think this happens because even though the true branch isn't used it tries to see if it's valid, and a sequence of void isn't valid.
A quick fix is to change void to any other type
var obj = Obj[bool](a: false)
Is this a bug? Should it be reported?No, it's just you not understanding types. ;-)
Obj[void] is an invalid type, the active case branch is irrelevant. Types capture the invariants of the data, types do not change depending on runtime values.
You could do something like:
type
Obj*[T] = object
case a*: bool
of false: discard
of true:
when T is void: discard
else:
b*: seq[T]
so the b field only exists when the type isnt bool. But in this case the variants seem pretty useless and you could just do:
type
Obj*[T] = object
when T is void:
discard
else:
b*: seq[T]