Get address from const

From manual :

You can get the address of variables, but you cannot use it for variables declared with let statements

I understand that this is done to ensure security. Now, if I want to get the address from const at any cost, is there a workaround?

+4
source share
1 answer

Consts really have no address; they cannot be saved at all. Take a look at this little program and see what happens in its intermediate C source code:

const x = 10
echo x
echo x + 1

The corresponding C code is as follows:

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 performed at compile time, and the final lines for echoare stored in the program instead of int x.

+6
source

All Articles