Why is `=` no function

Hey best community :slight_smile:

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

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? :thinking:

= (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.


Incidentally, R has such a function: assign.

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

If I recall correctly from a talk 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.


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.


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:

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

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…


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

    Because it doesn’t operate on the object, like what normal operator does. a = 3 has nothing to do with whatever previously in a, it’s not a mutation of the object in a but a, well, assignment to the name a. It operate on the binding/the name, which is not a first class thing in julia (and many scripting languages). C++, which has = as a normal operator, has = as operator because the variable is the object itself.

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

    It’s a choice for performance. A assignment done with a function is basically a local scope eval. It’s impossible to infer about.


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.


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

1 Like

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

No, not in Julia.

What would be the benefit of this?


No real benefit :slight_smile: just messing around.
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).


Haha :rofl:
<-- maybe. It’s just an idea.

In that case, you could just write a macro that does this.


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 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.


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;.

1 Like

What is their issue with that?

1 Like

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".

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.