Method forwarding macro

MacroTools.jl defines a @forward macro but it’s in the examples folder.

Lazy.jl exports its own version of the same macro 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?

There’s one in SpatialEcology.jl too, though it’s not exported I don’t think. It’s definitely not an ecology thing :stuck_out_tongue:

Something a tiny package would be good for?

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!

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

2 Likes

I would definitely use these :slight_smile:

This also seems related to the feature proposal by @cscherrer that I implemented for Reduce.jl

He also wanted a wrapper type which dispatches

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

1 Like

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

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.

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://white.ucc.asn.au/2019/04/03/Julia-Nomenclature.html#wrapper-types-and-delegation-pattern

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’

3 Likes

Yes. I thought they are the same, but interestingly, upon more research it appears that forwarding and delegation are different in their semantics.

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.

TypedDelegation.jl and Delegate.jl are two attempts to do just that.

1 Like