# Definition of unary operator inside a module, removes the \`Base\` definition

**URL:** https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371
**Category:** New to Julia
**Tags:** question
**Created:** [September 4, 2019, 12:02pm UTC](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371 "2019-09-04T12:02:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [September 4, 2019, 12:02pm UTC](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371/1 "2019-09-04T12:02:39Z")

</div>

i have a module in which a dispatch on `+` is defined

```julia
module DriftDiff
 function (+)(l::Layer, r::Layer)
  ...
 end

 function b()
  1.0+10.0
 end
end

```

If i call now the function `b()`

```julia
julia> DriftDiff.b()
ERROR: MethodError: no method matching +(::Float64, ::Float64)
You may have intended to import Base.+

```

How can i make `DriftDiff.b()` work again?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 4, 2019, 12:16pm UTC](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371/2 "2019-09-04T12:16:09Z")

</div>

What you did is to define a new function named `+`, which doesn’t remove the `Base` definition but [shadows it](https://en.wikipedia.org/wiki/Variable_shadowing) so that it is not visible as `+` within your module. This is a useful default because it prevents unexpected behaviors if you accidentally give a function the same name as some obscure `Base` function. You can still refer to the `Base` definition via `Base.:+`.

However, as the error message suggests, what you probably _meant_ to do is to _extend_ the `+` function in `Base` with a new method for your `Layer` type, rather than defining an entirely new function called `+`. One way to do this, as the error message suggests, is to explicitly `import` the `+` function before defining your method, via `import Base: +`. More commonly, you can specify that you are extending `Base.+` when you define your method:

```julia
function Base.:+(l::Layer, r::Layer)
  ...
end

```

(The special “quoted” syntax `Base.:+` is only required when extending operators, in order to disambiguate the syntax from a binary operation `Base .+ (...)`. You wouldn’t need it for adding methods to e.g. `Base.div`)

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [September 4, 2019, 12:23pm UTC](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371/3 "2019-09-04T12:23:18Z")

</div>

Excellent thanks

> One way to do this, as the error message suggests, is to explicitly `import` the `+` function before defining your method, via `import Base: +`

This was my problem, because the message says `import Base.+` and this I’ve tried without luck, now I know that it has to be ` Import Base:+`.

Thanks for teaching me !!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 4, 2019, 4:24pm UTC](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371/4 "2019-09-04T16:24:21Z")

</div>

> [@MatFi](#):
>
> This was my problem, because the message says `import Base.+` and this I’ve tried without luck, now I know that it has to be `Import Base:+` .

`import Base.+` should work (if you do it in the module before your function definition). However, if you want to qualify the function name itself, you have to quote it with `:` as `Base.:+`.

It seems a little inconsistent, so I filed a PR to fix both the error message and to allow quoting in `import` statements:

> <https://github.com/JuliaLang/julia/pull/33158>
>
> TLDR: This PR allows quoting in \`import\` statements, e.g. \`import Base.:+\`, simi…lar to other Julia contexts, and uses quoting for \`MethodError\` warnings about shadowed operators.
> 
> As was \[noted recently on discourse\](https://discourse.julialang.org/t/definition-of-unary-operator-inside-a-module-removes-the-base-definition/28371), the \`MethodError\` printing currently shows a potentially confusing suggestion when a function shadows a \`Base\` function if it is an operator, and in general the quoting of symbols (e.g. \`Base.+\` vs. \`Base.:+\`) was a bit inconsistent.
> 
> For example, if you accidentally shadow \`+\`, it prints \`You may have intended to import Base.+\`, which may be confusing because if you try to define the function as \`Base.+\` Julia will give an error. This PR changes the error message to quote operators.
> 
> Unfortunately, while \`function Base.:+(...)\` works, \`import Base.:+\` gave a syntax error, which seems a little inconsistent. So this PR also updates the parser to allow quoted symbols in \`import\` statements. Backwards compatible since it was a syntax error previously.
