Coming from R I am a bit puzzled by how namespaces are handled in Julia.
julia> cos(3)
-0.9899924966004454
julia> sin = 0
0
julia> cos = 0
ERROR: cannot assign a value to variable Base.cos from module Main
Stacktrace:
[1] top-level scope
@ REPL[3]:1
julia> Base.sin(3)
0.1411200080598672
julia> Base.exp(3)
20.085536923187668
julia> exp = 0
0
I do understand what’s happening here, there is some lazy loading going on, so I can assign to sin
, but not to cos
because I’ve already used it.
However, I have not indicated explicitly anywhere that I want to assign to Base.cos, and indeed, that’s not what I do when I assign to sin
. And not to exp
, presumably because I used the Base.
prefix.
Although this is not a big deal, it’s symptomatic of a broader problem. There are hundreds of functions in Base, and in other modules. Many of which one is not aware of. If, by ignorance, you use one of these names for your own variable, and then later modifies your code to use the function of the same name in Base (or an upgrade suddenly adds that name to Base), things break down.
Moreover, it also discourages the use of commonly used names in modules, because it can interfere with users’ code, as in https://github.com/JuliaLang/julia/issues/35538
I don’t have a solution to this, though I know that in R similar issues are solved by having different namespace lookup-tables for function calls and variables. I.e. you can assign a variable in your current namespace, but if you call it as a function, a search for a function in your attached namespaces is performed. (But then, in R, non-function objects are not callable as in Julia.)