Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using recurse with keywords #152

Open
christopher-dG opened this issue Oct 11, 2019 · 0 comments
Open

Using recurse with keywords #152

christopher-dG opened this issue Oct 11, 2019 · 0 comments

Comments

@christopher-dG
Copy link
Contributor

Related to #48, mentioned on Slack and @vchuravy encouraged me to open an issue.

using Cassette: Cassette

Cassette.@context Ctx

# Here's the "normal" case where we don't accept keyword arguments

f(_args...) = println("f")

function Cassette.overdub(ctx::Ctx, ::typeof(f), args...)
    if false  # But really, sometimes true.
        :hi
    else
        # When there are no keywords, everything is fine.
        Cassette.recurse(ctx, f, args...)
    end
end

# Cassette.overdub(Ctx(), () -> f(1))

# And here's the weird one with kwargs

g(_args...; _kwargs...) = println("g")

Cassette.overdub(ctx::Ctx, ::Core.kwftype(typeof(g)), kwargs, ::typeof(g), args...) =
    Cassette.overdub(ctx, g, args...; kwargs...)

function Cassette.overdub(ctx::Ctx, ::typeof(g), args...; kwargs...)
    if false  # But really, sometimes true.
        :hi
    else
        # How can I recurse into f with keywords?
        # 1. This throws an exception:
        #   Cassette.recurse(ctx, g, args...; kwargs...)
        # 2. This just loops back to this point, causing a stack overflow:
        #   Cassette.recurse(ctx, () -> g(args...; kwargs...))
        "????????????"
    end
end

# Cassette.overdub(Ctx(), () -> g(; x=1))

To me, this is expected behaviour:

Option 1 shouldn't work because recurse is not defined with keyword arguments.

Option 2 recurses into a new anon function, which then calls g(args...; kwargs...), which reaches overdub(ctx::Ctx, ::Core.kwftype(typeof(g)), kwargs, ::typeof(g), args...), which calls overdub(ctx::Ctx, ::typeof(g), args...; kwargs...), which once again hits that recurse. So infinite recursion is justified.

The real solution here is to somehow be able to pass keywords to recurse. Is there a way that we can define a method of recurse that accepts keywords, similar to what we can do with overdub?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant