This code:

static:
  var foo = "test"
echo foo

generates broken code:

2 $  nim compile test_static.nim
Hint: used config file '/home/bkerin/opt/nim-0.17.2/config/nim.cfg' [Conf]
Hint: used config file '/home/bkerin/.config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test_static [Processing]
gcc -c  -w  -I/home/bkerin/opt/nim-0.17.2/lib -o /home/bkerin/projects/upcb/nimcache/test_static.o /home/bkerin/projects/upcb/nimcache/test_static.c
Error: execution of an external compiler program 'gcc -c  -w  -I/home/bkerin/opt/nim-0.17.2/lib -o /home/bkerin/projects/upcb/nimcache/test_static.o /home/bkerin/projects/upcb/nimcache/test_static.c' failed with exit code: 1

/home/bkerin/projects/upcb/nimcache/test_static.c: In function ‘NimMainModule’:
/home/bkerin/projects/upcb/nimcache/test_static.c:104:19: error: ‘foo_gGf7nGBFMKl3DkCtR0dVkg’ undeclared (first use in this function)
  printf("%s\012", foo_gGf7nGBFMKl3DkCtR0dVkg? (foo_gGf7nGBFMKl3DkCtR0dVkg)->data:"nil");
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/bkerin/projects/upcb/nimcache/test_static.c:104:19: note: each undeclared
identifier is reported only once for each function it appears in

It would be quite handy for me to be able to use some stuff built up at compile-time again to do some corresponding run-time work. I guess it would all need to turn into const Is there some simple way to do that?

2017-11-14 05:02:22

hmm looks like this does work:

static:
  var foo = "imastring"

const bar = foo

echo bar

Makes sense I guess but seems a little clunky and you have to invent a new name and probably find the deep copy. They should maybe just automagically turn into const a the end of compile, they're still in the namespace anyway.

2017-11-14 05:20:03

currently there is no simple way to make compile-time var to exist at run-time.

some attempt has been taken place to enhance the compiler capability by enabling all compile-time var exist at run-time. But that solution is not optimal, because not everyone want all compile-time var also exist at run-time.

The idea to introduce new pragma to annotate compile-time var was rejected by araq. He said, the compiler must be smart enough to identify which compile-time var need to exist at run-time without introducing new pragma.

therefore, we need to make the compiler smarter. the compiler should be able to know which compile-time var accessed at both compile-time and run-time, and which one is accessed only at compile-time. the compiler then generate run-time code for the former and generate nothing for the latter.

the general idea is simple, but the implementation is not so simple.

2017-11-14 10:24:17