compiles() doesn't like an assignment that works outside of compiles(). What am I not understanding?
type
Foo = distinct int32
Bar = distinct int32
converter toBar(x: Foo): Bar = Bar(x)
when isMainModule:
var a = Foo(8)
var b = Bar(0)
b = Foo(41) # This works
assert compiles(b = Foo(42)) # Compiler throws AssertionDefect
Nim Compiler Version 2.2.0 (windows and macOS)
That's because the argument to a call must be and expression and in nim, assignments are statements, not expressions. But you can use a statement list expression to get what you want:
assert compiles((b = Foo(42);))
or alternatively use command syntax:
assert:
compiles:
b = Foo(42)