hello, i’m sort of new to odin.
can someone explain why does this code not work?
and how to make it work?
x2 :: proc "contextless" ($x: int) -> int {
return x * 2;
}
four :: x2(2); // Error: not a compile time constant
main :: proc() {
fmt.println("Hello Number", four);
}
Just to give some more context: A :: constant in odin is not just an immutable value like a const is in some languages like JavaScript or Zig. In odin, a constant is a value that is known at compile time. From the docs:
The constant’s value must be able to be evaluated at compile time
For people coming from C, Odin’s :: is closer to #define than it is static const.
Also, like Bill said, odin does not support compile time execution. Putting both of those pieces together means that you cannot call a function to initialize a const.
Edit: To make your code work, initialize the value at runtime: