# Macro to overload Base operators

**URL:** <https://discourse.julialang.org/t/macro-to-overload-base-operators/6698>\
**Category:** General Usage\
**Created:** [October 26, 2017, 9:34am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698 "2017-10-26T09:34:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [October 26, 2017, 9:34am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698/1 "2017-10-26T09:34:09Z")

</div>

I wish to generate code like

```julia
Base.:(<)(a::∂ℝ{N},b::∂ℝ{N}) where{N} = <(a.x,b.x)

```

for all possible operators, and all sorts of combinations of input types. I need the “Base.” qualifier because I am in a module. I write a little code generator

```julia
for OP in (:(>),:(<),:(==),:(>=),:(<=),:(!=)) #...
   eval(quote
      $OP(a::∂ℝ{N},b::∂ℝ{N}) where{N} = $OP(a.x,b.x)
      $OP(a::ℝ ,b::∂ℝ{N}) where{N} = $OP(a ,b.x)
      $OP(a::∂ℝ{N},b::ℝ ) where{N} = $OP(a.x,b )
   end)
end

```

The problem I see is that my macro generates something like

```julia
<(a::∂ℝ{N},b::∂ℝ{N}) where{N} = <(a.x,b.x)

```

that is, my “Base.” qualifier is missing. I have tried to modify my metacode to do that, without success:

```julia
Base.:( $OP)(a::∂ℝ{N},b::∂ℝ{N}) where{N} = $OP(a.x,b.x)

```

does not do the trick. “LoadError: \< not defined for…”

And I would like to add: same question in a macro, where I should replace OP with esc(OP) .

---

<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 26, 2017, 10:55am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698/2 "2017-10-26T10:55:31Z")

</div>

Try to create a MWE ([How to create a Minimal, Reproducible Example - Help Center - Stack Overflow](https://stackoverflow.com/help/mcve)), something that is copy-paste-able. That usually gets more attention 🙂

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [October 26, 2017, 11:07am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698/3 "2017-10-26T11:07:35Z")

</div>

Thanks Fredrike

Here we go: this code fails. To make it run, one must make the code to generate functio definitions qualified by “Base.”. But how?

Philippe

```julia
module mymod
export mytype

struct mytype
    x :: Float64
end

macro Op1(OP)
    return quote
        $OP(a::mytype) = mytype($OP(a.x)) # how to generate Base.OP, including macro hygiene?
    end
end
@Op1 cos
@Op1 sin
#...

for OP in (:(>),:(<),:(==),:(>=),:(<=),:(!=)) 
    eval(quote
        $OP(a::mytype,b::mytype) = $OP(a.x,b.x) # how to generate Base.OP
    end)
end

end # module

using mymod
a = mytype(1.)
b = mytype(2.)
@show a < b
@show cos(a)

```

---

<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:** [October 26, 2017, 11:15am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698/4 "2017-10-26T11:15:47Z")

</div>

Use `Base.$OP` in your macro and in the `eval`

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Philippe\_Maincon](https://discourse.julialang.org/u/Philippe_Maincon)\
**Post date:** [October 26, 2017, 11:18am UTC](https://discourse.julialang.org/t/macro-to-overload-base-operators/6698/5 "2017-10-26T11:18:04Z")

</div>

Well that was silly.

Not the answer mind you, but that I did not try this!!!

😀

Merci Cédric, ca marche maintenant!
