# Why is \`=\` no function

**URL:** <https://discourse.julialang.org/t/why-is-no-function/47304>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [September 26, 2020, 10:03am UTC](https://discourse.julialang.org/t/why-is-no-function/47304 "2020-09-26T10:03:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 26, 2020, 10:03am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/1 "2020-09-26T10:03:09Z")

</div>

Hey best community 🙂

I was wondering, way `=` can’t be used like `+` or `*`.

I would like to do something like `=(:a,3)`, `typeof(=)` and `assign = =`.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 26, 2020, 11:14am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/2 "2020-09-26T11:14:34Z")

</div>

> [@Impressium](#):
>
> I would like to do something like `=(:a,3)` , `typeof(=)` and `assign = =` .

Not totally clear about what you meant here. Nevertheless, can I ask why you want 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:** [September 26, 2020, 11:50am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/3 "2020-09-26T11:50:53Z")

</div>

> [@Impressium](#):
>
> do something like `=(:a,3)`

`=` (the assignment, not `setindex!`) can’t be a function because it has other implications (eg scope). A variable `a` has no first-class existence independent of it scope.

It is unclear what you are trying to do, but I second @tomerarnon that clarifying the context would help.

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [September 26, 2020, 12:08pm UTC](https://discourse.julialang.org/t/why-is-no-function/47304/4 "2020-09-26T12:08:38Z")

</div>

Incidentally, R has such a function: `assign`.

```nohighlight
> assign('a', 3)
> a
[1] 3

```

If I recall correctly from a [talk](https://www.youtube.com/watch?v=HStF1RJOyxI&t=135s) by Jan Vitek a few years ago (highly recommended, by the way) this function was provided as an example of a serious obstacle to performance enhancements in R.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 26, 2020, 12:32pm UTC](https://discourse.julialang.org/t/why-is-no-function/47304/5 "2020-09-26T12:32:49Z")

</div>

I think that’s because in `R`, functions can in some ways act as macros do in Julia. You can totally write a macro in Julia that does an assignment as well, but doing this at runtime is generally a really bad idea for performance, because it doesn’t give the compiler any chance to specialize code. Julia doesn’t allow function calls to create new local bindings, but you could theoretically create new global variables using `eval`. Needless to say, there are pretty much always better ways to achieve what you want than using `eval` in functions.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 26, 2020, 1:18pm UTC](https://discourse.julialang.org/t/why-is-no-function/47304/6 "2020-09-26T13:18:05Z")

</div>

What’s maximally scary about `assign` isn’t just that it’s a function that does assignment – it’s that it lets you specify the scope in which the assignment occurs:

```julia
> x <- 1
>
> foo <- function() {
+ assign("x", 2, envir = globalenv())
+ }
>
> foo()
>
> x
[1] 2

```

---

<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:** [September 26, 2020, 1:28pm UTC](https://discourse.julialang.org/t/why-is-no-function/47304/7 "2020-09-26T13:28:20Z")

</div>

> [@johnmyleswhite](#):
>
> ```julia
> + assign("x", 2, envir = globalenv())
> 
> ```

Well, `eval` does exactly this… Parent scope is the one that’s annoy. Python has that so I assume R does too but I don’t really know…

> `=(:a,3)`

You also realized that the corresponding syntax should be `=(a, 3)` instead right? This alone makes it impossible to behave like a normal operator.

There are a few related issues here.

1. Why `=` can’t be a normal operator

2. Why assignment can’t be done with a function using a different syntax

---

<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:** [September 26, 2020, 1:38pm UTC](https://discourse.julialang.org/t/why-is-no-function/47304/8 "2020-09-26T13:38:57Z")

</div>

I’ll also add that allowing specifically `=(a, 3)` to behave like `a = 3` wouldn’t have any of the issues I mentioned. However, that’ll just be a syntax trick. You cannot expect `typeof(=)` or `f = =; f(a, 3)` to work the way you expect so there’s little point to allow that.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 28, 2020, 8:26am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/9 "2020-09-28T08:26:22Z")

</div>

I guess because it’s cool and it would fit all the other great feature of the language 🙂

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 28, 2020, 8:29am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/10 "2020-09-28T08:29:45Z")

</div>

In a sense `a = 1` is already a spezial kind of function. `=(:a, 1)` would just be a different kind to write it down. In the end both would compile down to the same thing, I guess

---

<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:** [September 28, 2020, 8:33am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/11 "2020-09-28T08:33:37Z")

</div>

> [@Impressium](#):
>
> In a sense `a = 1` is already a spezial kind of function.

No, not in Julia.

> [@Impressium](#):
>
> `=(:a, 1)` would just be a different kind to write it down. In the end both would compile down to the same thing, I guess

What would be the benefit of this?

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 28, 2020, 9:20am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/12 "2020-09-28T09:20:55Z")

</div>

No real benefit 🙂 just messing around.  
My initial thought was to define `<- = =` so that I can do `a <- 1`.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 28, 2020, 10:22am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/13 "2020-09-28T10:22:58Z")

</div>

> [@Impressium](#):
>
> My initial thought was to define `<- = =` so that I can do `a <- 1` .

That also won’t work because `<-` is not an operator. `a <- 1` is parsed as `a < (-)(1)`.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 28, 2020, 10:25am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/14 "2020-09-28T10:25:57Z")

</div>

Haha 🤣  
`<--` maybe. It’s just an idea.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 28, 2020, 10:34am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/15 "2020-09-28T10:34:32Z")

</div>

In that case, you could just write a macro that does 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:** [September 28, 2020, 11:31am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/16 "2020-09-28T11:31:14Z")

</div>

I agree with @simeonschaub that a macro could help with this, possibly using one of the Unicode operators that visually resemble `<-`, eg `←`), and walking the AST with a macro. [Precedence](https://github.com/JuliaLang/julia/blob/b89c4a3cb539ed47eca651cf290a20adde552fc2/src/julia-parser.scm#L35) could be tricky though, as pairs and conditionals are between assignment and arrows. Also, what about `+=` etc.

FWIW, I think that using `<-` for assignment is one of the more horrible choices made for R. It’s not like they didn’t have `=` free for this purpose.

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 28, 2020, 11:38am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/18 "2020-09-28T11:38:05Z")

</div>

Sorry about that digression but, what about having `<-` or `←` for teaching purpose? I feel like this could help students to overcome their issue with `a = 1; a = 2;`.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 28, 2020, 11:45am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/19 "2020-09-28T11:45:42Z")

</div>

> [@remi-garcia](#):
>
> I feel like this could help students to overcome their issue with `a = 1; a = 2;` .

What is their issue with that?

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 28, 2020, 11:48am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/20 "2020-09-28T11:48:59Z")

</div>

`a` cannot be equal to `1` and to `2`. I was helping a student that had an initiation to CS and I encounter this: “But, how could `a` be equal to `2` if it is equal to `1`”.

---

<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:** [September 28, 2020, 11:56am UTC](https://discourse.julialang.org/t/why-is-no-function/47304/21 "2020-09-28T11:56:23Z")

</div>

I think it is just best to get over this quickly. Explain that `=` means assignment, and proceed with the course.

IMO teaching this any other way is doing students a disservice, as they will have to switch to `=` at some point anyway.

[Next page](https://discourse.julialang.org/t/why-is-no-function/47304.md?page=2)
