Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Flux.@functor #2546

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ end

macro functor(args...)
@warn """The use of `Flux.@functor` is deprecated.
Most likely, you should write `Flux.@layer MyLayer` which will add various convenience methods for your type,
Most likely, you should write `Flux.@layer MyLayer`\
which will add various convenience methods for your type,\
such as pretty-printing and use with Adapt.jl.
However, this is not required. Flux.jl v0.15 uses Functors.jl v0.5, which makes exploration of most nested `struct`s
opt-out instead of opt-in... so Flux will automatically see inside any custom struct definitions.
However, this is not required. Flux.jl v0.15 uses Functors.jl v0.5,\
which makes exploration of most nested `struct`s opt-out instead of opt-in...\
so Flux will automatically see inside any custom struct definitions.
If you really want to apply the `@functor` macro to a custom struct, use `Functors.@functor` instead.
""" maxlog=1

return Functors.functorm(args...)
# From https://discourse.julialang.org/t/calling-a-macro-from-within-a-macro-revisited/19680
return esc(:($Functors.@functor($(args...))))
end

# Allows caching of the parameters when params is called within gradient() to fix #2040.
Expand Down
13 changes: 13 additions & 0 deletions test/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@
ps = Flux.params([2,3])
@test length(ps) == 1
end

@testset "Flux.@functor" begin
# https://github.com/FluxML/Flux.jl/issues/2545
struct A2545; x; y; end
Flux.@functor A2545
a = A2545(1, 2)
@test fmap(x -> 2x, a) == A2545(2, 4)

struct B2545; x; y; end
Flux.@functor B2545 (x,)
b = B2545(1, 2)
@test fmap(x -> 2x, b) == B2545(2, 2)
end
Loading