# Weird scoping issue

**URL:** <https://discourse.julialang.org/t/weird-scoping-issue/14162>\
**Category:** General Usage\
**Tags:** scope\
**Created:** [August 27, 2018, 11:41pm UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162 "2018-08-27T23:41:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [August 27, 2018, 11:41pm UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/1 "2018-08-27T23:41:09Z")

</div>

using Julia `v"1.0.0"`,

```julia
julia> count = 0
0

```

or

```julia
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).

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 28, 2018, 12:56am UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/2 "2018-08-28T00:56:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 28, 2018, 4:25am UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/3 "2018-08-28T04:25:19Z")

</div>

This is not a scope issue at all.

> [@Nosferican](#):
>
> Why is it that the function is not exported/defined until called?

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

> [@Nosferican](#):
>
> If one hasn’t call it, it gets overwritten in the namespace ( `Base.count` is still available).

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.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [August 28, 2018, 4:30am UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/4 "2018-08-28T04:30:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 28, 2018, 5:12am UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/5 "2018-08-28T05:12:39Z")

</div>

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
julia> Base.count
count (generic function with 6 methods)

julia> count = 0
0

```

But (in a new REPL)

```julia
julia> count
count (generic function with 6 methods)

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

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 28, 2018, 12:09pm UTC](https://discourse.julialang.org/t/weird-scoping-issue/14162/6 "2018-08-28T12:09:52Z")

</div>

> [@Nosferican](#):
>
> it is called

It’'s just used, not called.

> [@Nosferican](#):
>
> Is the overwrite only if the namespace hasn’t been looked up exclusive to Base?

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.

> [@jlperla](#):
>
> this doesn’t error if you call `Base.count` instead of `count` .

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.
