I was implementing a proc which accepted a nil value for a sequence as default parameter. But the idiom of assigning the variable locally and checking for nil surprised me with a compilation error:
type Tannoying = object
tempt: bool
tease: string
proc test(a: seq[Tannoying] = nil) =
var a = a
if a == nil: a = @[]
echo repr(a)
when isMainModule:
test()
The compiler expects the comparison line to invoke the == proc, so adding this will calm the compiler:
proc `==`(a, b: Tannoying): bool =
if a.tempt == b.tempt and a.tease == b.tease:
result = true
It is however strange, since another option to calm the compiler would be to remove the if and change the default parameter for the list from nil to @[] directly. I wonder why of the difference between the nil/empty list case, why do I need a comparison proc for object/sequence creation, and if it can be avoided, or at least display a less menacing compiler error.
For the moment I'll go with the default @[] parameter since that requires less code and moves the if check to the compiler generating the call.