ForwardMethods.jl: Composition made easy(ish) (I'm not the author)

I’m not the author of this package but I found this really useful in my work just now and I thought it deserves some more love. It allows you to forward a number of methods for your wrapper types in one macro, and even implement entire standard interfaces! My use case was creating a “world line” type, mapping time values to events, and I wanted additional methods on my type. Rather than having to implement the whole dict interface myself, I found this package and it was as simple as doing this.

julia> using DataStructures, ForwardMethods

julia> struct W{T, V} d::SortedDict{T, V} end

julia> @forward_interface W{T, V} field=d interface=dict

julia> function W{T, V}()
           return W{T, V}(SortedDict{T, V}())
       end

julia> w[1.0] = 1
1

julia> w[2.0] = -1
-1

julia> w[0.5] = -1
-1

julia> w
W{Float64, Int8}(SortedDict{Float64, Int8, Base.Order.ForwardOrdering}(0.5 => -1, 1.0 => 1, 2.0 => -1))

I thought it was just really neat. Julia has quite a lot of really good packages that just get abandoned (through no fault of anybody), and I think just showing some acknowledgement for small useful packages would be nice. Post any other packages y’all think don’t get enough love. I’d love to start using them.

P.S, I feel like defining more interfaces for this package would be super cool. I may look at trying to add the interface for AbstractGraph myself as a PR!

12 Likes

This package looks incredible useful! I hadn’t heard about it, thanks for sharing

Pinging the author @curtd so they know :slight_smile:

2 Likes

Thank you for the very kind words! I made it to reduce my own day-to-day boilerplate and I’m happy it’s useful for other folks as well.

6 Likes

Just curious, do you think the package could be written without using generated functions? AFAIU they are not ideal in regards to precompilation (although perhaps they are not an issue in this case)

Hmm so the generated function usage is there for creating the very generic properties interface. Everything else used on the macro-side of things is just for expression generation. So if you don’t use that particular interface for any of your types, you shouldn’t encounter any precompilation issues with the package.

1 Like