# Can we override "="?

**URL:** <https://discourse.julialang.org/t/can-we-override/5856>\
**Category:** General Usage\
**Created:** [September 12, 2017, 5:04pm UTC](https://discourse.julialang.org/t/can-we-override/5856 "2017-09-12T17:04:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [September 12, 2017, 5:04pm UTC](https://discourse.julialang.org/t/can-we-override/5856/1 "2017-09-12T17:04:57Z")

</div>

Topic says it all. If so how?  
Thanks

---

<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:** [September 12, 2017, 5:21pm UTC](https://discourse.julialang.org/t/can-we-override/5856/2 "2017-09-12T17:21:32Z")

</div>

No.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [September 12, 2017, 5:25pm UTC](https://discourse.julialang.org/t/can-we-override/5856/3 "2017-09-12T17:25:23Z")

</div>

I think the closest options are:

- Override `a[]=4` with `setindex!`.
- Use a macro to make `@changeequals a=4` do whatever you want.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 13, 2017, 12:44am UTC](https://discourse.julialang.org/t/can-we-override/5856/4 "2017-09-13T00:44:16Z")

</div>

Actually I don’t think that the title does say it all: What do you actually want to do and why? It’s likely that there is a different / better solution.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [September 13, 2017, 3:24am UTC](https://discourse.julialang.org/t/can-we-override/5856/5 "2017-09-13T03:24:36Z")

</div>

On the other hand, the OP may want enlightenment rather than practical advice. @kristoffer.carlsson’s “No” follows from the fact that assignment is not a Julia function. In this respect, Julia differs from other (more-or-less) functional languages, and one assumes there is a good reason for this design. I have looked around for an explanation, without success. Perhaps @jeff.bezanson (or any other ur-developer) would be kind enough to restate the reasoning here.

---

<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 13, 2017, 3:54am UTC](https://discourse.julialang.org/t/can-we-override/5856/6 "2017-09-13T03:54:29Z")

</div>

This is pretty standard among scripting languages where `=` is always change of binding. Such semantics cannot possibly be implemented as a function and assignment is also used often enough that it will severely affect the predictability/inferability of the code if it’s behavior can be changed.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [September 13, 2017, 4:11am UTC](https://discourse.julialang.org/t/can-we-override/5856/7 "2017-09-13T04:11:21Z")

</div>

Seems totally possible to have an assign function which takes the new name wrapped in Val?

---

<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 13, 2017, 4:47am UTC](https://discourse.julialang.org/t/can-we-override/5856/8 "2017-09-13T04:47:52Z")

</div>

No. A function cannot do assignment.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [September 13, 2017, 4:54am UTC](https://discourse.julialang.org/t/can-we-override/5856/9 "2017-09-13T04:54:33Z")

</div>

I mean possible in the sense that it wouldn’t interfere with the compiler because the exact name of every assignment would be known to it.

---

<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 13, 2017, 5:02am UTC](https://discourse.julialang.org/t/can-we-override/5856/10 "2017-09-13T05:02:19Z")

</div>

It will interfere a lot since you still have to dispatch on the type (of RHS). Plus it’s basically never what assignment overload is overloaded on for the LHS.

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [September 13, 2017, 10:25am UTC](https://discourse.julialang.org/t/can-we-override/5856/11 "2017-09-13T10:25:55Z")

</div>

While not exactly the same as overriding `=`, you could of course write a macro to parse the forms passed and do a transformation on any `Expr` with `:(=)` as its `head`.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [September 13, 2017, 7:36pm UTC](https://discourse.julialang.org/t/can-we-override/5856/12 "2017-09-13T19:36:29Z")

</div>

First, you have to distinguish mutating assignment from simply binding names to values. Mutating assignment (e.g. to arrays, and possibly in the future to structs, `a.b = c`) can indeed be overloaded.

But binding is much more primitive. If binding were done by a function, how would the bindings for the arguments to _that_ function be established? Clearly there would need to be some simpler, more primitive form of binding for that. So we choose to simplify the language by only exposing that more primitive form.

I think this makes programs easier to reason about, for the human and the computer. In `a = f(x)`, you can know that `a` will refer exactly to the value of `f(x)`. This is a powerful abstraction: you can name and juggle values around however you want, and nobody can tell! There is an airtight separation between how values are computed, and how consumers of those values refer to them.
