# Using a macro to define two methods at once?

**URL:** <https://discourse.julialang.org/t/using-a-macro-to-define-two-methods-at-once/27239>\
**Category:** Specific Domains\
**Tags:** metaprogramming\
**Created:** [August 6, 2019, 8:26pm UTC](https://discourse.julialang.org/t/using-a-macro-to-define-two-methods-at-once/27239 "2019-08-06T20:26:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 6, 2019, 8:26pm UTC](https://discourse.julialang.org/t/using-a-macro-to-define-two-methods-at-once/27239/1 "2019-08-06T20:26:50Z")

</div>

there is a way to define two methods of a function with a macro?, for example, a macro like this:

```julia
f = @mymacro (2,3)

```

that generates the following code:

```julia
f(x) = 2*x
f(x,y) = 2*x+3*y

```

the example is simple and what i’m looking is different, but the key is to generate two methods for `f`, using a different amount of variables

---

<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:** [August 6, 2019, 11:05pm UTC](https://discourse.julialang.org/t/using-a-macro-to-define-two-methods-at-once/27239/2 "2019-08-06T23:05:45Z")

</div>

As always with a macro, you need to write code that will return a Julia expression object corresponding to the code you want to be compiled. In this case that expression object should correspond to defining these two functions. You can do this eg inside a quote…end block.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 7, 2019, 3:14am UTC](https://discourse.julialang.org/t/using-a-macro-to-define-two-methods-at-once/27239/3 "2019-08-07T03:14:26Z")

</div>

I have a macro [here](https://github.com/baggepinnen/MonteCarloMeasurements.jl/blob/1d7dd9f38c3de9ae606b94b9c12d4d5d32edeac5/src/forwarddiff.jl#L24)  
that takes a definition like `f(x::T1,y::T2)` and defines both that and `f(y::T2, x::T1)`
