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

Multiple @kwcalls redefines methods causing warnings #16

Closed
mileslucas opened this issue May 23, 2021 · 2 comments · Fixed by #18
Closed

Multiple @kwcalls redefines methods causing warnings #16

mileslucas opened this issue May 23, 2021 · 2 comments · Fixed by #18

Comments

@mileslucas
Copy link
Collaborator

JuliaAstro/Transits.jl#23 (comment)

Calling @kwcall multiple times on the same method name will create warnings in module code. This is happening because

q = quote
KeywordCalls._call_in_default_order(::typeof($f), nt::NamedTuple{($(sorted_args...),)}) = $f(NamedTuple{($(args...),)}(nt))
$f(nt::NamedTuple) = KeywordCalls._call_in_default_order($f, _sort(merge($defaults, $alias($f, nt))))
$f(; kw...) = $f(merge($defaults, $alias($f, NamedTuple(kw))))
end
adds the two foo(::NamedTuple) and foo(; kwargs...) calls, regardless if they exist already.

@mileslucas
Copy link
Collaborator Author

Could this be handled with hasmethod?

q = quote
    KeywordCalls._call_in_default_order(::typeof($f), nt::NamedTuple{($(sorted_args...),)}) = $f(NamedTuple{($(args...),)}(nt))
	if !hasmethod($f, Tuple{NamedTuple})
	    $f(nt::NamedTuple) = KeywordCalls._call_in_default_order($f, _sort(merge($defaults, $alias($f, nt))))
	end
	if !hasmethod($f, Tuple{})
	    $f(; kw...) = $f(merge($defaults, $alias($f, NamedTuple(kw))))
	end
end

@cscherrer
Copy link
Owner

Thanks for catching this! This seems like the right idea. Let's see if we can do this with the hasmethod checks outside the quote, roughly like

q = quote
    KeywordCalls._call_in_default_order(::typeof($f), nt::NamedTuple{($(sorted_args...),)}) = $f(NamedTuple{($(args...),)}(nt))
end

if !hasmethod(f, Tuple{NamedTuple})
    push!(q.args, :($f(nt::NamedTuple) = KeywordCalls._call_in_default_order($f, _sort(merge($defaults, $alias($f, nt)))))
end

if !hasmethod(f, Tuple{})
    push!(q.args, :($f(; kw...) = $f(merge($defaults, $alias($f, NamedTuple(kw)))))
end

If we need the hasmethod in the generated code, we can try static_hasmethod from Tricks.jl

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

Successfully merging a pull request may close this issue.

2 participants