I have a function that uses a constant parameter. The parameter never changes between function calls. For example:
function radtodeg(x) const PI = 3.14159 return x * 180 / PI end
PI is a constant that doesn’t change between function calls. Moreover, it is only used by the function (
PI is a bad example in this regard), so it makes no sense to declare it as a global constant. Also, it makes no sense to have the caller pass the constant variable pre-allocated, because this is a parameter that only makes sense internally in the function.
A more complicated example could use a constant array instead of just a number. I don’t want to have to reallocate this variable every time the function is called.
In C++, you could mark such constant parameters as
const static. I understand that a proposal for static variables was rejected:
So what is the proper way to deal with this scenario? Or maybe Julia’s compiler is smart enough to detect this condition, and allocates
PI only once?