Skip to content

Commit be3b7ee

Browse files
fix Flux.@functor (#2546)
1 parent 40b7f70 commit be3b7ee

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/deprecations.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ end
9999

100100
macro functor(args...)
101101
@warn """The use of `Flux.@functor` is deprecated.
102-
Most likely, you should write `Flux.@layer MyLayer` which will add various convenience methods for your type,
102+
Most likely, you should write `Flux.@layer MyLayer`\
103+
which will add various convenience methods for your type,\
103104
such as pretty-printing and use with Adapt.jl.
104-
However, this is not required. Flux.jl v0.15 uses Functors.jl v0.5, which makes exploration of most nested `struct`s
105-
opt-out instead of opt-in... so Flux will automatically see inside any custom struct definitions.
105+
However, this is not required. Flux.jl v0.15 uses Functors.jl v0.5,\
106+
which makes exploration of most nested `struct`s opt-out instead of opt-in...\
107+
so Flux will automatically see inside any custom struct definitions.
106108
If you really want to apply the `@functor` macro to a custom struct, use `Functors.@functor` instead.
107109
""" maxlog=1
108-
109-
return Functors.functorm(args...)
110+
# From https://discourse.julialang.org/t/calling-a-macro-from-within-a-macro-revisited/19680
111+
return esc(:($Functors.@functor($(args...))))
110112
end
111113

112114
# Allows caching of the parameters when params is called within gradient() to fix #2040.

test/deprecations.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
ps = Flux.params([2,3])
33
@test length(ps) == 1
44
end
5+
6+
@testset "Flux.@functor" begin
7+
# https://github.com/FluxML/Flux.jl/issues/2545
8+
struct A2545; x; y; end
9+
Flux.@functor A2545
10+
a = A2545(1, 2)
11+
@test fmap(x -> 2x, a) == A2545(2, 4)
12+
13+
struct B2545; x; y; end
14+
Flux.@functor B2545 (x,)
15+
b = B2545(1, 2)
16+
@test fmap(x -> 2x, b) == B2545(2, 2)
17+
end

0 commit comments

Comments
 (0)