You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/index.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ For large models it can be cumbersome or inefficient to work with parameters as
8
8
9
9
## Basic Usage and Implementation
10
10
11
-
When one marks a structure as [`@functor`](@ref) it means that Functors.jl is allowed to look into the fields of the instances of the struct and modify them. This is achieved through [`Functors.fmap`](@ref).
11
+
By default, julia types are marked as [`@functor`](@ref)s, meaning that Functors.jl is allowed to look into the fields of the instances of the struct and modify them. This is achieved through [`Functors.fmap`](@ref).
12
12
13
-
The workhorse of fmap is actually a lower level function, functor:
13
+
The workhorse of `fmap` is actually a lower level function, functor:
14
14
15
15
```julia-repl
16
16
julia> using Functors
@@ -20,8 +20,6 @@ julia> struct Foo
20
20
y
21
21
end
22
22
23
-
julia> @functor Foo
24
-
25
23
julia> foo = Foo(1, [1, 2, 3]) # notice all the elements are integers
26
24
27
25
julia> xs, re = Functors.functor(foo)
@@ -50,12 +48,17 @@ julia> fmap(float, model)
50
48
Baz(1.0, 2)
51
49
```
52
50
53
-
Any field not in the list will be 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.
51
+
Any field not in the list will be passed through as-is during reconstruction. This is done by invoking the default constructor accepting all fields as arguments, so structs that define custom inner constructors are expected to provide one that acts like the default.
54
52
55
-
## Appropriate Use
53
+
The use of `@functor` with no fields argument as in `@functor Baz` is equivalent to `@functor Baz fieldnames(Baz)`
54
+
and also equivalent to avoiding `@functor` altogether.
55
+
56
+
Using [`@leaf`](@ref) instead of [`@functor`](@ref) will prevent the fields of a struct from being traversed.
56
57
57
-
!!! warning "Not everything should be a functor!"
58
-
Due to its generic nature it is very attractive to mark several structures as [`@functor`](@ref) when it may not be quite safe to do so.
58
+
!!! warning "Change to opt-out behaviour in v0.5"
59
+
Previous releases of functors, up to v0.4, used an opt-in behaviour where structs were not functors unless marked with `@functor`. This was changed in v0.5 to an opt-out behaviour where structs are functors unless marked with `@leaf`.
60
+
61
+
## Appropriate Use
59
62
60
63
Typically, since any function `f` is applied to the leaves of the tree, but it is possible for some functions to require dispatching on the specific type of the fields causing some methods to be missed entirely.
0 commit comments