Weird scoping issue

using Julia v"1.0.0",

julia> count = 0
0

or

julia> count
count (generic function with 6 methods)

julia> count = 0
ERROR: cannot assign variable Base.count from module Main

Why is it that the function is not exported/defined until called? If one hasn’t call it, it gets overwritten in the namespace (Base.count is still available).

1 Like

This is an interesting issue, I think it might be related to how it is impossible to “import” a method after it has been called first. For example, try making a call to + and then try importing + from another module. You will find that the import is only successful if you did not make a call to + first. I suspect this is by design, because otherwise issues would happen. This allows methods to be imported if they are unused, and locks them into dispatch after being used.

This is not a scope issue at all.

It is, and you never called it in this case.

The correct way to say it is that before you use it, you are allowed to overwrite it. This makes exporting new function non-breaking.

1 Like

In the second session it is called with count which achieves the same as ?count or count([true, false]).

I feel this is sort of R’s promised functionality. Is the overwrite only if the namespace hasn’t been looked up exclusive to Base?

1 Like

Another curious behavior to make sure we understand is that this doesn’t error if you call Base.count instead of count. That is,

julia> Base.count
count (generic function with 6 methods)

julia> count = 0
0

But (in a new REPL)

julia> count
count (generic function with 6 methods)

julia> count = 0
ERROR: cannot assign variable Base.count from module Main

It’'s just used, not called.

No. You are also not looking it up in Base in your case anyway. You are looking it up in Main. It’s just imported from Base, which isn’t special.

Right. (Well, again, you aren’t calling it, you are just using it). After you use the imported name, you can’t overwrite that import anymore.