As of
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2026-02-06
Copyright (c) 2006-2026 by Andreas Rumpf
git hash: 12a2333817ad8864cbb2e36367701c908631c787
active boot switches: -d:release
this example
type Foo = distinct int
proc `=copy`(a: var Foo, b: Foo) =
if cast[int](a) == cast[int](b):
return
copyMem(addr a, addr b, sizeof(Foo))
echo "copy!"
type Bar = object
val: Foo
let x = Bar(val: Foo(13)) does not compile (example.nim(14, 12) Error: '=dup' is not provided while a custom '=copy' is defined for type 'Foo'). The analogous example with object instead of distinct throws a deprecation warning instead.
If my understanding is correct, a custom =dup allows to skip the self-assignment check. In overwhelming number of cases, the performance impact is trivial. Am I missing something? If not, why force an additional hook and break a non-trivial amount of code?