# Method forwarding macro

**URL:** https://discourse.julialang.org/t/method-forwarding-macro/23355
**Category:** General Usage
**Created:** [April 20, 2019, 6:21pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355 "2019-04-20T18:21:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [April 20, 2019, 6:21pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/1 "2019-04-20T18:21:06Z")

</div>

MacroTools.jl defines a [`@forward` macro but it’s in the examples folder](https://github.com/MikeInnes/MacroTools.jl/blob/master/src/examples/forward.jl).

Lazy.jl exports [its own version of the same macro](https://github.com/MikeInnes/Lazy.jl/blob/a1b5f06ade07a442f0c009fb0bb4fe594db11fd2/src/macros.jl#L264-L288) but it’s not documented in the README.

Come to think of it, while the `@forward` macro is quite useful, it isn’t really a macro development tool nor a functional programming tool. So I’m not too surprised that it is treated like a second-class citizen in those packages.

Any thought about what kind of package would be a good fit for this?

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [April 21, 2019, 12:23am UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/2 "2019-04-21T00:23:23Z")

</div>

> [@tk3369](#):
>
> Come to think of it, while the `@forward` macro is quite useful, it isn’t really a macro development tool nor a functional programming tool.

[There’s one in `SpatialEcology.jl`](https://github.com/EcoJulia/SpatialEcology.jl/blob/1a0fb0e9f86da0fe10de5eb12cb5b26462fd5c2d/src/ComMatrix.jl) too, though it’s not exported I don’t think. It’s _definitely_ not an ecology thing 😛

Something a tiny package would be good for?

---

<div class="post-metadata">

### Author: ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)
#### Post date: [April 21, 2019, 5:27am UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/3 "2019-04-21T05:27:10Z")

</div>

It’s so trivial to implement, is there really a need for a package to do this? I have my own version of this, and I’m very happy using it. Perhaps we should take a lesson from the NPM leftpad fiasco and avoid distributing lots of trivial packages.

On the other hand, some kind of `OoHelpers.jl` might have a place that includes macros for method forwarding, implementing the “Holy trait” pattern and other OOP helpers–but not classical inheritance, for the love of God. [A line must be drawn, here! This far and no further!](https://youtu.be/s3RNsZvdYZQ?t=1m26s)

I might think about working on something like that, along with some documentation about Julia’s approach to OO.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [April 21, 2019, 1:27pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/4 "2019-04-21T13:27:19Z")

</div>

> [@ninjaaron](#):
>
> macros for method forwarding, implementing the “Holy trait” pattern

I would definitely use these 🙂

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [April 21, 2019, 1:35pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/5 "2019-04-21T13:35:58Z")

</div>

This also seems related to the feature proposal by @cscherrer that I implemented for [Reduce.jl](https://github.com/chakravala/Reduce.jl)

> [@Type constraints in Distributions and StatsFuns](https://discourse.julialang.org/t/type-constraints-in-distributions-and-statsfuns/22663/12):
>
> Alright, so I went ahead and [implemented](https://github.com/chakravala/Reduce.jl/commit/33f234cbc9560142b57f7bf95a8041dd0f91152f) the requested feature, which works on master branch julia\> Reduce.@subtype FakeReal \<: Real julia\> FakeReal(R"x+1") + FakeReal(R"y") y + 1 + x the implementation looks like this at the moment macro subtype(x) x.head ≠ :\<: && throw(error("$x is not a subtype expression")) name = x.args[1] Expr(:struct,false,x,Expr(:block,Expr(:(::), :r, :RExpr))) |\> eval @eval begin export $name show(io::IO, r::$name) = show(io,r.r) …

He also wanted a wrapper type which dispatches

Personally, I dont like to depend on MacroTools, and prefer writing custom code transformations.

---

<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: [April 21, 2019, 2:29pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/6 "2019-04-21T14:29:24Z")

</div>

> [@tk3369](#):
>
> [its own version of the same macro](https://github.com/MikeInnes/Lazy.jl/blob/a1b5f06ade07a442f0c009fb0bb4fe594db11fd2/src/macros.jl#L264-L288) but it’s not documented in the README.

I you are missing that, then a PR documenting it there would be the most straightforward solution.

> [@tk3369](#):
>
> it isn’t really a macro development tool

Of course, but implementing it using MacroTools is rather easy, which is why I suspect it is in Lazy.jl, which already depends on MacroTools. Of course it is possible to do it from scratch, too.

One could make a mini-package that only has some version of `@forward`, and perhaps nothing else, or maybe a bit of related functionality. If there is a commitment to maintaining it, I would consider using `@forward` from there, but it would be great if it was a drop-in replacement for `Lazy.@forward`.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [April 21, 2019, 3:58pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/7 "2019-04-21T15:58:22Z")

</div>

Isn’t this what is more commonly referred to as delegation?  
if so MacroTools and Lazy.jl are being variant in the naming

E.g.

[https://github.com/JuliaArbTypes/TypedDelegation.jl](https://github.com/JuliaArbTypes/TypedDelegation.jl)

[https://github.com/Jeffrey-Sarnoff/Delegate.jl](https://github.com/Jeffrey-Sarnoff/Delegate.jl)

[https://github.com/JuliaCollections/DataStructures.jl/blob/master/src/delegate.jl](https://github.com/JuliaCollections/DataStructures.jl/blob/master/src/delegate.jl)

[https://white.ucc.asn.au/2019/04/03/Julia-Nomenclature.html#wrapper-types-and-delegation-pattern](https://white.ucc.asn.au/2019/04/03/Julia-Nomenclature.html#wrapper-types-and-delegation-pattern)

[http://wiki.c2.com/?DelegationIsInheritance](http://wiki.c2.com/?DelegationIsInheritance)

To my mind delegation is at the core of polymorphism via composition.  
But it is surprisingly hard to generalize exactly what you want to do with it though.

Which is why you’ll see lots of code that is implementing it,  
with some variations either with custom macros or loops over methods calling `@eval’

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [April 21, 2019, 6:58pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/8 "2019-04-21T18:58:51Z")

</div>

Yes. I thought they are the same, but interestingly, upon more research it appears that [forwarding and delegation are different in their semantics](https://stackoverflow.com/a/28539643/1576462).

I believe that composition is used in many places in Julia but it may be that most people just write the delegation code manually. It would be nice to have a standard macro somewhere that is versatile enough to cover many use cases.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [April 21, 2019, 8:40pm UTC](https://discourse.julialang.org/t/method-forwarding-macro/23355/9 "2019-04-21T20:40:54Z")

</div>

> [@tk3369](#):
>
> . It would be nice to have a standard macro somewhere that is versatile enough to cover many use cases.

[TypedDelegation.jl](https://github.com/JuliaArbTypes/TypedDelegation.jl) and [Delegate.jl](https://github.com/Jeffrey-Sarnoff/Delegate.jl) are two attempts to do just that.
