Calling functions in constants

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);
}

Odin does not support compile time execution. That’s why it does not work.

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:

four := x2(2)

thanks gingerBill and GigaGrunch, that makes sense.
i appreciate it.

wait a minute so you can run arbitrary code to initialize variables?

package main

import "core:c/libc"

x2 :: proc "contextless" (x: int) -> int {
    libc.fprintf(libc.stderr, "x2(%d)\n", x);
    return x * 2;
}

four := x2(2);
six := x2(3);

main :: proc() {
    libc.fprintf(libc.stderr, "Hello Number %d\n", six);
    libc.fprintf(libc.stderr, "Hello Number %d\n", four);
}

woah it works, that is cool :0

anyways thanks a lot

wait a minute so you can run arbitrary code to initialize variables?

There is also a special annotation for procs to perform this kind of initialization: @(init) I think this is the preferred way.