8

@static expr is a way to only run expr once; for some sense of once.
but what sense of once is it?

Is it:

  • once per time the package is installed?
  • once per time the package is loaded?
  • some other definition

The most common use is for OS checking: e.g. ccall((@static Sys.iswindows() ? :_fopen : :fopen), ...)

I am wondering if I can use it to generate different code based on an environment variable: In particular JULIA_NUM_THREADS. This environment variable can change between runs of julia, but if it changes during the session nothing will react to it.

Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137

1 Answers1

5

It is run at parse time (technically it is run just after parse time, when macros are expanded). If used in a package with precompilation enabled (the default), then it will be evaluated the first time it is loaded (i.e. when you see the message "INFO: Precompiling module ...").

So no, you can't use it to generate different code based on environmental variables, unless you explicitly use __precompile__(false) outside the module (however then you will have longer loading times).

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50