@@ -30,6 +30,47 @@ function functorm(T, fs = nothing)
3030 :(makefunctor (@__MODULE__ , $ (esc (T)), $ (fs... )))
3131end
3232
33+ """
34+ @functor T
35+ @functor T (x,)
36+
37+ Adds methods to [`functor`](@ref) allowing recursion into objects of type `T`,
38+ and reconstruction. Assumes that `T` has a constructor accepting all of its fields,
39+ which is true unless you have provided an inner constructor which does not.
40+
41+ By default all fields of `T` are considered [`children`](@ref);
42+ this can be restricted be restructed by providing a tuple of field names.
43+
44+ # Examples
45+ ```jldoctest
46+ julia> struct Foo; x; y; end
47+
48+ julia> @functor Foo
49+
50+ julia> Functors.children(Foo(1,2))
51+ (x = 1, y = 2)
52+
53+ julia> _, re = Functors.functor(Foo(1,2));
54+
55+ julia> re((10, 20))
56+ Foo(10, 20)
57+
58+ julia> struct TwoThirds a; b; c; end
59+
60+ julia> @functor TwoThirds (a, c)
61+
62+ julia> ch2, re3 = Functors.functor(TwoThirds(10,20,30));
63+
64+ julia> ch2
65+ (a = 10, c = 30)
66+
67+ julia> re3(("ten", "thirty"))
68+ TwoThirds("ten", 20, "thirty")
69+
70+ julia> fmap(x -> 10x, TwoThirds(Foo(1,2), Foo(3,4), 56))
71+ TwoThirds(Foo(10, 20), Foo(3, 4), 560)
72+ ```
73+ """
3374macro functor (args... )
3475 functorm (args... )
3576end
0 commit comments