struct Foo
end
@is_friend(Foo, "Bob")
@is_friend(Foo, "Tim")
println(friends_of(Foo))
Is there a clean way of implementing @is_friend and friends_of to return ["Bob", "Tim"]?
One solution is with a global Dict. Then @is_friend becomes push!(that_dict[Foo], "Bob") and friends_of is trivial. But that will Revise very poorly, and it doesn’t work with precompilation (unless I use Requires.@init, but ehhh…)
Another solution is for @is_friend to become is_friend(::Type{Foo}, ::Val{"Bob"}) = true. Then I can read the friend list off of methods((Type{Foo}, Any,)). That is Revise- and precompilation-friendly. But it’s a terrible solution… I guess?
Am I missing anything here? (Other than “You should rewrite your program in a different way”)
It’s not open. Suppose that instead of friends, I’m registering methods that should be called by Foo at some point. I’m stuck doing a big ugly @register_all_methods Foo begin ... end that contains all the methods in one big block. I have that solution already. @big_macro_that_contains_complex_expansion_code_to_register_all_methods_from_dsl_code. It’s not great. It would be much nicer and modular with a smaller @declare_fruminating_method macro that macroexpands into @register_method Foo ...