# Puzzling behaviour when assigning to variables which are function names

**URL:** <https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053>\
**Category:** Internals & Design\
**Created:** [June 9, 2020, 9:40am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053 "2020-06-09T09:40:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [June 9, 2020, 9:40am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053/1 "2020-06-09T09:40:16Z")

</div>

Coming from R I am a bit puzzled by how namespaces are handled in Julia.

```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](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.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 9, 2020, 11:08am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053/2 "2020-06-09T11:08:35Z")

</div>

> [@sgaure](#):
>
> there is some lazy loading going on, so I can assign to `sin` , but not to `cos` because I’ve already used it.

Exactly.

> [@sgaure](#):
>
> I have not indicated explicitly anywhere that I want to assign to Base.cos

Since you used it, it is now in your namespace as a function.

> [@sgaure](#):
>
> 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.

Sure, but you will get an error message so you can investigate and fix.

> [@sgaure](#):
>
> discourages the use of commonly used names in modules, because it can interfere with users’ code

Users can selectively import only what they need, eg

```julia
using ThatModule: foo

```

or just use a qualified

```julia
ThatModule.foo

```

> [@sgaure](#):
>
> in R similar issues are solved by having different namespace lookup-tables for function calls and variables

Yes, [R is Lisp-2 and Julia is Lisp-1](https://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace) (corrected, thanks @sgaure). It is a design choice, both alternatives have advantages and disadvantages; you can find extensive discussions about this.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 9, 2020, 11:10am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053/3 "2020-06-09T11:10:31Z")

</div>

Eg one of the classical papers is [Gabriel (1988)](http://www.nhplace.com/kent/Papers/Technical-Issues.html).

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [June 9, 2020, 11:42am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053/4 "2020-06-09T11:42:40Z")

</div>

> [@Tamas\_Papp](#):
>
> Yes, [R is Lisp-1 and Julia is Lisp-2](https://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace)

It’s the opposite, I believe. R is Lisp-2 (CL), Julia is Lisp-1 (Scheme). My point is that for a general language with a large number of general packages with functions that users may not be aware of, it is a problem for package developers to try and avoid names which users might like to use for their own purposes. It doesn’t help a lot that avoidance of name-clashes depends on lazy loading, rather it can add to the confusion.

For types it’s less of a problem, because the tradition is to capitalize them, whereas user variables are not.

But, as you say, everything has pros and cons.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 9, 2020, 11:51am UTC](https://discourse.julialang.org/t/puzzling-behaviour-when-assigning-to-variables-which-are-function-names/41053/5 "2020-06-09T11:51:45Z")

</div>

The point of this is so that code that looks like

```julia
using Foo
bar = 2

```

will not break if `Foo` starts exporting a variable called `bar`.
