MikeInnes / Lazy.jl

I was gonna maintain this package, but then I got high
Other
469 stars 54 forks source link

Support keyword arguments in forward macro #112

Closed tk3369 closed 4 years ago

tk3369 commented 4 years ago

Quick test:

using MacroTools

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...; kwargs...) = (Base.@_inline_meta; $f(x.$field, args...; kwargs...)))
        for f in fs]...);
    nothing)
end

struct Foo1 end
play(x::Foo1; y) = y

struct Foo2 
    f::Foo1
end
@forward Foo2.f play

f1 = Foo1()
play(f1, y = 2)

f2 = Foo2(f1)
play(f2, y = 3)
pfitzseb commented 4 years ago

Cool. Could you add that test to the package tests?

tk3369 commented 4 years ago

Mmm... the tests have been added but it failed CI because I cannot define a struct inside @testset. Looks like it's a feature added in Julia 1.1.

ERROR: LoadError: error compiling top-level scope: type definition not allowed inside a local scope

Do you think we should just move the struct definition to the top level? Else, we can consider updating the TravisCI config to a later version.

tk3369 commented 4 years ago

ping @pfitzseb

pfitzseb commented 4 years ago

Should be fine to move the struct definition out of the testset.

tk3369 commented 4 years ago

@pfitzseb I think we're ready to merge. Please take a look and let me know if there's any issues. Thanks

pfitzseb commented 4 years ago

Thanks for the contribution!