Skip to content

Commit 17487ff

Browse files
committed
Updated explanation in README
1 parent a6ef462 commit 17487ff

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ Foo(1.0, [1.0, 2.0, 3.0])
4949

5050
`functor` returns the parts of the object that can be inspected, as well as a `re` function that takes those values and restructures them back into an object of the original type.
5151

52-
For a discussion regarding implementing functors for which only a subset of the fields are "seen" by `functor`, see [here](https://github.com/FluxML/Functors.jl/issues/3#issuecomment-626747663).
52+
To include only certain fields, pass a tuple of field names to `@functor`:
53+
54+
```julia
55+
julia> struct Baz
56+
x
57+
y
58+
end
59+
60+
julia> @functor Baz (x,)
61+
62+
julia> model = Baz(1, 2)
63+
Baz(1, 2)
64+
65+
julia> fmap(float, model)
66+
Baz(1.0, 2)
67+
```
68+
69+
Any field not in the list will not be returned by `functor` and passed through as-is during reconstruction. This is done by invoking the default constructor, so structs that define custom inner constructors are expected to provide one that acts like the default.
70+
71+
It is also possible to implement `functor` by hand when greater flexibility is required. See [here](https://github.com/FluxML/Functors.jl/issues/3) for an example.
5372

5473
For a discussion regarding the need for a `cache` in the implementation of `fmap`, see [here](https://github.com/FluxML/Functors.jl/issues/2).

0 commit comments

Comments
 (0)