# Redefining x - y = 0

**URL:** https://discourse.julialang.org/t/redefining-x-y-0/17015
**Category:** General Usage
**Tags:** question
**Created:** [October 31, 2018, 5:10pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015 "2018-10-31T17:10:03Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 31, 2018, 5:10pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/1 "2018-10-31T17:10:03Z")

</div>

I just realized you can do this in Julia:

```julia
x - y = 0

```

subsequently breaking the minus sign. I had a bug in my code where I wrote `-` instead of `_` with the intention of defining a variable, and broke `-` instead. It was a bit hard to find.

Should there be a warning or something when you overwrite a Base function like this?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [October 31, 2018, 5:14pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/2 "2018-10-31T17:14:18Z")

</div>

Its a feature 😛

[Infix operator definition syntax needs documentation · Issue #15483 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15483)

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [October 31, 2018, 7:22pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/3 "2018-10-31T19:22:46Z")

</div>

To avoid this kind of typo we could require spaces like here

```julia
julia> false?0:1
ERROR: syntax: space required before "?" operator

julia> false ?0:1
ERROR: syntax: space required after "?" operator

julia> false ? 0:1
ERROR: syntax: space required before colon in "?" expression

julia> false ? 0 :1
ERROR: syntax: space required after colon in "?" expression

julia> false ? 0 : 1
1

```

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [November 1, 2018, 2:48am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/4 "2018-11-01T02:48:49Z")

</div>

I encountered this same issue in my code a few years ago and complained about it on this discourse. It comes up about twice a year. I proposed that there should be some extra syntax required when defining a type-pirating “core” operation that already has a definition in `Base`. I argued that imposing extra syntactic requirements in this case is necessary to prevent users from shooting themselves in the foot. However, the core-devs disagreed with me. To paraphrase, they said that since this is perfectly legal Julia behavior, there is no reason to make anyone jump through a syntactic hoop to harness it, and too bad for the users who accidentally redefine methods because of a typo. I continue to believe that extra syntactic clues should be mandatory in this case, but it is probably too late to revisit the issue.

---

<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: [November 1, 2018, 3:05am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/5 "2018-11-01T03:05:53Z")

</div>

> [@Stephen\_Vavasis](#):
>
> However, the core-devs disagreed with me. To paraphrase, they said that since this is perfectly legal Julia behavior, there is no reason to make anyone jump through a syntactic hoop to harness it, and too bad for the users who accidentally redefine methods because of a typo.

That is not a fair nor correct “paraphrasing”. The thread where the discussion took place has already been linked, no need for you to go and put words into peoples mouth. Just link to the dicsussions instead.

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [November 1, 2018, 4:07am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/6 "2018-11-01T04:07:51Z")

</div>

You are absolutely correct, and I apologize for putting words in other people’s mouths. There are 4 or 5 different discourse threads bearing on this issue; I will try to track them down and post links to them in the next couple of days.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 1, 2018, 4:17am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/7 "2018-11-01T04:17:53Z")

</div>

The main objector was @jeff.bezanson and he makes the case that uniformity of syntax is more important than making ad hoc syntax exceptions. At no point did he make any statements about not caring about users. In any case, changing this would be breaking, so there’s not much point in discussing it except in the context of Julia 2.0. I don’t think that it would be necessary to dig up links.

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 1, 2018, 4:56am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/8 "2018-11-01T04:56:58Z")

</div>

I guess you could write your own little function that reads a .jl file as a string and then goes hunting for common typo char patterns, maybe highlight the bits it finds with `printstyled`. It probably wouldn’t need to run that often though

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [November 1, 2018, 1:25pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/9 "2018-11-01T13:25:40Z")

</div>

First, I want to apologize again. I misquoted the core-devs and simplified a complex issue into an inappropriate and demeaning sound-bite. I found two previous threads on this topic; there are maybe 2-3 others that I can’t find right now.

> [@Unexpected behavior of #==#](https://discourse.julialang.org/t/unexpected-behavior-of/2891):
>
> Hi, I was playing around with multi line comments (in Julia v0.5.1) , and got some interesting results. So I tried: julia\> my#=comment=#var = 0 (generic function with 1 method) And now I get: julia\> 3 \* 4 0 So I just overloaded (\*) for (Any,Any). That’s weird. So, I wanted to retry in a way, that I can edit \*, so: import Base.\* my#=comment=#var = 0 This creates a new method for (\*), so “hi” \* false returns now 0. When I try this in Jupyter via Juliabox (Version 0.5), I get an error…

> [@Cannot use size(::Array{Int, 2}): ERROR: LoadError: UndefRefError: access to undefined reference](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138):
>
> I keep getting undefreferror whenever I call function size. Here is my code. The error is at the first if in function forward. Full error message is: ERROR: LoadError: UndefRefError: access to undefined reference in #forward#55(::Array{Any,1}, ::Function, ::NN.CrossEntropyLoss, ::Array{Float64,2}, ::Array{Int64,2}) at /Users/haonanchen/Documents/CS/NN.jl/src/layers/CrossEntropyLoss.jl:37 in forward(::NN.CrossEntropyLoss, ::Array{Float64,2}, ::Array{Int64,2}) at /Users/haonanchen/Documents/CS/…

With respect to Stefan Karpinski’s comment about not caring about users, I believe that the core-devs care very much about the user community. Indeed, Stefan personally deserves a medal for his patient and careful explanations on this discourse site! And the core-devs make huge numbers of other contributions to the user-base, including answering issues on github, arranging JuliaCon… the list is very long.

This issue is not about caring versus not caring about users; rather it is about balancing competing interests of two different groups of users. For the purpose of explanation, let me propose a straw-man “new syntax rule” (NSR): If a function `g` defined in an inner scope has the same name as an existing function `g` in an outer scope, then at least one argument of the inner `g` must have its type qualified (`::Any` is OK). Zero-argument functions are exempt.

For a power user who writes code-generation macros and other forms of introspective code (user group A), NSR would be a hindrance and an annoying speed bump.

For a Julia newbie, for a university-classroom user, and for a person prone to typos (user group B), NSR could save that person an hour or two of chasing down a “spooky-action-at-a-distance” bug caused by a typo.

My contention is that the current state of affairs in which a one-character typo can redefine an operator favors user group A too much over user group B.

---

<div class="post-metadata">

### Author: ![hckr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hckr/32/4836_2.png) [@hckr](https://discourse.julialang.org/u/hckr)
#### Post date: [November 1, 2018, 1:51pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/10 "2018-11-01T13:51:49Z")

</div>

I thought this kind of definition would be a good joke for April Fool’s day. Just put something like `x + y = "Send bitcoins to get your computer back..." ` into the beginning of your friends’ `.juliarc`. This shadows all definitions of `+`, so you can enjoy the show.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 1, 2018, 2:09pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/11 "2018-11-01T14:09:45Z")

</div>

I got a bit confused here, because I thought you had to import `-` before extending it. But it’s more complicated than that.

\<Starting a new Julia instance\>

```julia
julia> -(a, b) = println("Bye")
- (generic function with 1 method)

julia> 2 - 3
Bye

```

\<Shutting down and restarting\>

```julia
julia> 2 - 3
-1

julia> -(a, b) = println("Bye")
ERROR: error in method definition: function Base.- must be explicitly imported to be extended
Stacktrace:
 [1] top-level scope at none:0

```

I was expecting the error in both scenarios.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 1, 2018, 2:19pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/12 "2018-11-01T14:19:15Z")

</div>

The first one creates a new function named `-` in the REPL module `Main` and does not modify the core `-` function. In the second session you use core `-` in the REPL first so it cannot subsequently be shadowed.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 1, 2018, 2:23pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/13 "2018-11-01T14:23:11Z")

</div>

Yes, that would have been my guess.

I just thought that all exported functions in Base (or Core?) would have been loaded into Main, and could therefore not be shadowed. That would have made the current problem unlikely to occur.

Why isnẗ this the case?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 1, 2018, 2:24pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/14 "2018-11-01T14:24:47Z")

</div>

> [@DNF](#):
>
> Why isnẗ this the case?

Because it is _very nice_ to be able to define module local functions with the same name as functions in Base.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 1, 2018, 7:58pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/15 "2018-11-01T19:58:22Z")

</div>

> [@fredrikekre](#):
>
> Because it is _very nice_ to be able to define module local functions with the same name as functions in Base.

As far as I can see, this is nice mostly when do not shadow the function, but rather extend it, as in

```julia
struct Q x::Int end
Base.:(-)(x::Q, y::Q) = Q(x.x - y.y)

```

What use case could you have for shadowing (instead of extending) a function like `+`? Or in general, shadowing any loaded function (even if it has not been called)?

Wouldn’t it be safer that shadowing required special syntax?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 1, 2018, 8:08pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/16 "2018-11-01T20:08:03Z")

</div>

> [@e3c6](#):
>
> As far as I can see, this is nice mostly when do not shadow the function, but rather extend it, as in

No, I often shadow `Base` functions (maybe not `+` but say `size` or something). There is also packages like `JSON.jl` that have their own `JSON.parse` function which is distinct from `Base.parse` for example.

> [@e3c6](#):
>
> Wouldn’t it be safer that shadowing required special syntax?

No? It is much safer that special syntax (`import Base: *` or `Base.:*(...)`) is required when extending since that effects the global behavior.

---

<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: [November 2, 2018, 2:29pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/17 "2018-11-02T14:29:05Z")

</div>

> [@e3c6](#):
>
> Should there be a warning or something when you overwrite a Base function like this?

As others have said, defining a function with the same name as some other function in `Base` is occasionally useful (note that unless you `import`, you are not adding/overwriting a method to the original function).

After tracking down a bug, especially if it is hard to find, it is tempting to think of various safeguards that would have prevented it, but this is not always a good guide for refining a language; as one can easily end up with a straitjacket replacing a powerful language.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [November 2, 2018, 3:18pm UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/18 "2018-11-02T15:18:13Z")

</div>

Maybe a warning could be issued by a linter, at least for simple cases like when it sees `a-var=2`, which can be a typo for `a_var=2`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 3, 2018, 8:02am UTC](https://discourse.julialang.org/t/redefining-x-y-0/17015/19 "2018-11-03T08:02:01Z")

</div>

I think that the linter should tell you to write

```julia
a - var = 2

```

instead of

```julia
a-var=2

```

Then you would discover the bug.

It’s a really bad habit to mash variables and operators together like that. It makes code hard to read, and, as demonstrated in this thread, more bug-prone.
