# Coding style in unary operator overloading

**URL:** <https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888>\
**Category:** General Usage\
**Tags:** style, operator\
**Created:** [December 16, 2024, 2:22pm UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888 "2024-12-16T14:22:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [December 16, 2024, 2:22pm UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/1 "2024-12-16T14:22:17Z")

</div>

Is there a reason to prefer one of the following two options? Option 1:

```julia
Base.:-(a::MyType) = MyType(-a.data)

```

Option 2:

```julia
import Base.-
-(a::MyType) = MyType(-a.data)

```

---

<div class="post-metadata">

**Author:** ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)\
**Post date:** [December 16, 2024, 3:16pm UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/2 "2024-12-16T15:16:12Z")

</div>

Ultimately, I think it comes down to your specific context and coding preferences.

1: Maybe If you’re only extending `Base.:-(...)` and not calling `Base.-` elsewhere, this avoids unnecessary imports.  
2: Maybe If you’re overloading multiple operators, using `import Base.op` for each operator at the top can improve readability.

---

<div class="post-metadata">

**Author:** ![savq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/savq/32/22063_2.png) [@savq](https://discourse.julialang.org/u/savq)\
**Post date:** [December 17, 2024, 1:16am UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/3 "2024-12-17T01:16:53Z")

</div>

I prefer writing the module when adding a method because it’s easier to grep.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [December 17, 2024, 1:36am UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/4 "2024-12-17T01:36:33Z")

</div>

Second, @savq. You’re also less likely to accidentally pirate methods by using a name that you forgot you had imported.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 17, 2024, 2:35am UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/5 "2024-12-17T02:35:35Z")

</div>

Agreed, but I make an exception for operators. `Base.:-(a::MyType)` is an eyesore, and besides I would never define my own version of `-` distinct from Base. Are there any packages that do that? Sounds like a nightmare!

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [December 17, 2024, 8:50am UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/6 "2024-12-17T08:50:51Z")

</div>

> [@cstjean](#):
>
> I would never define my own version of `-` distinct from Base. Are there any packages that do that?

Any package which defines custom number-like objects, such as Symbolics.jl and Mods.jl.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 17, 2024, 10:42am UTC](https://discourse.julialang.org/t/coding-style-in-unary-operator-overloading/123888/7 "2024-12-17T10:42:09Z")

</div>

> [@mrufsvold](#):
>
> You’re also less likely to accidentally pirate methods by using a name that you forgot you had imported.

Or accidentally define a new function when the symbol is no longer imported (accidental deletion of import statement, package’s export list changes and affects the draft `using` statements). For the same reasons, reassigning an imported name outright errors without a properly qualified module. That makes it the primary purpose for me.

> [@greatpet](#):
>
> Any package which defines custom number-like objects, such as [Symbolics.jl](https://juliahub.com/ui/Packages/General/Symbolics)

Nope.

```julia
julia> parentmodule(Symbolics.:-)
Base

```
