Skip to content

Instantly share code, notes, and snippets.

Created Apr 21, 2019
What would you like to do?
Method forwarding macro from Lazy
@forward T.x functions...
Define methods for `functions` on type `T`, which call the relevant function
on the field `x`.
# Example
struct Wrapper
@forward Wrapper.x Base.sqrt # now sqrt(Wrapper(4.0)) == 2.0
@forward Wrapper.x Base.length, Base.getindex, Base.iterate # several forwarded functions are put in a tuple
@forward Wrapper.x (Base.length, Base.getindex, Base.iterate) # equivalent to above
macro forward(ex, fs)
@capture(ex, T_.field_) || error("Syntax: @forward T.x f, g, h")
T = esc(T)
fs = isexpr(fs, :tuple) ? map(esc, fs.args) : [esc(fs)]
:($([:($f(x::$T, args...) = (Base.@_inline_meta; $f(x.$field, args...)))
for f in fs]...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment