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

Replace KeywordDispatch for an in-house implementation #272

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mofeing
Copy link
Member

@mofeing mofeing commented Dec 26, 2024

KeywordDispatch.jl is no longer actively developed and doesn't propagate docs. With our own implementation of keyword-dispatching, we have more control over it and can fix the propagation of docs.

The idea is to do sth as Core.kwcall; i.e. Julia lowers calls to methods with keyword-arguments to calls to Core.kwcall where the 1st argument is a NamedTuple with the arguments, the 2nd argument is the type of the function and the rest are the positional arguments. In our case, we call that kwcall manually on the default method without a kwcall function (but I can reconsider), so we implement keyword-dispatched methods as a method of the same function but where the kwargs are a NamedTuple that is passed as the first argument.

For example, for inds, the replacement for @kwdispatch is

inds(tn::AbstractTensorNetwork; kwargs...) = inds(sort_nt(values(kwargs)), tn)

The default @kwmethod is replaced for

inds(::@NamedTuple{}, tn::AbstractTensorNetwork) = inds((; set=:all), tn)

And the set-kwarg method is now

function inds(kwargs::NamedTuple{(:set,)}, tn::AbstractTensorNetwork)
    tn = TensorNetwork(tn)
    if kwargs.set === :all
...

It looks a lil more verbose than before, but we can add some helpers. The nice thing is that we can now put docstrings to these keyword-methods and also point to them.

Note: sort_nt is a function that sorts the keys of a NamedTuple. It's used for making keyword-dispatch invariant to kwarg order.

@mofeing mofeing added this to the 0.8 milestone Dec 26, 2024
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 this pull request may close these issues.

1 participant