4

From the manual:

One can get the address of variables, but one can't use it on variables declared through let statements

I understand this is done to provide safety. Now, if i want to get the address from a const at all cost is there a workaround?

Arrrrrrr
  • 802
  • 6
  • 13

1 Answers1

6

Consts really don't have an address, they may not even be stored anywhere at all. Let's look at this small program and see what happens in its intermediate C source code:

const x = 10
echo x
echo x + 1

The relevant C code looks like this:

STRING_LITERAL(TMP5, "10", 2);
STRING_LITERAL(TMP6, "11", 2);

NIM_EXTERNC N_NOINLINE(void, xInit)(void) {
    printf("%s\012", ((NimStringDesc*) &TMP5)? (((NimStringDesc*) &TMP5))->data:"nil");
    printf("%s\012", ((NimStringDesc*) &TMP6)? (((NimStringDesc*) &TMP6))->data:"nil");
}

So the calculation is actually done at compile time and the final strings for echo are stored in the program instead of the int x.

def-
  • 5,275
  • 20
  • 18