In the following code assertion is failed. Looks like a bug. Should I create an issue?
import macros
type
Kind = enum kind1, kind2
Foo = object
case kind: Kind
of kind1: val1: int
of kind2: val2: int
macro test(node: untyped): auto =
quote do: Foo(kind: kind1, val1: `node`)
const a = test(123)
var b = test(123)
echo "const a = ", a
echo " var b = ", b
assert(a.val1 == 123)
assert(b.val2 == 123)
Output:
var a = (kind: kind1, val1: 123)
const b = (kind: kind1, val1: 0)
Expected output:
var a = (kind: kind1, val1: 123)
const b = (kind: kind1, val1: 123)
Yes,
There are a couple of issues with variants set at compile-time and then used at runtime:
i.e. created at compile-time, used at compile-time works, created at runtime, used at runtime works, but created at compile-time and used at runtime has issues in certain cases. In your codebase you should add some asserts/tests just in case.
Can't we do the other way around, start supporting case objects properly across compile-time to runtime boundary?
That would be very useful for both nimbus, as a core component is a let and instead could be inline as a const, and Laser where I defined my own AST to be used at runtime (with LLVM) and compiletime (with Nim macros) and not being able to cross the boundary would be a restriction.
GCC 4.7 and older successfully compiles this:
union S {
int a;
float b;
};
int main() {
const S s = {.b = 1.0};
}