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

Return empty NamedTuple in @functor instead of empty Tuple #36

Merged
merged 1 commit into from
Nov 15, 2022
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
4 changes: 2 additions & 2 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function makefunctor(m::Module, T, fs = fieldnames(T))
escfs = [:($f = getfield(x, $(QuoteNode(f)))) for f in fs]

@eval m begin
$Functors.functor(::Type{<:$T}, x) = ($(escfs...),), y -> $T($(escargs...))
$Functors.functor(::Type{<:$T}, x) = (;$(escfs...)), y -> $T($(escargs...))
end
end

Expand All @@ -30,7 +30,7 @@ macro functor(args...)
functorm(args...)
end

isleaf(x) = children(x) === ()
isleaf(@nospecialize(x)) = children(x) === ()

children(x) = functor(x)[1]

Expand Down
13 changes: 11 additions & 2 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ struct NoChild{T}; x::T; end
### Basic functionality
###

@testset "Children and Leaves" begin
no_children = NoChildren2(1, 2)
has_children = Foo(1, 2)
@test Functors.isleaf(no_children)
@test !Functors.isleaf(has_children)
@test Functors.children(no_children) == ()
@test Functors.children(has_children) == (x=1, y=2)
end

@testset "Nested" begin
model = Bar(Foo(1, [1, 2, 3]))

Expand All @@ -44,7 +53,7 @@ end
@testset "Property list" begin
model = OneChild3(1, 2, 3)
model′ = fmap(x -> 2x, model)

@test (model′.x, model′.y, model′.z) == (1, 4, 3)
end

Expand Down Expand Up @@ -77,7 +86,7 @@ end
m4 = (x=1, y=(2, 3), z=4:5)
@test isbits(fmap(float, m4))
@test_skip 0 == @allocated fmap(float, m4) # true, but fails in tests

# Shared mutable containers are preserved, even if all children are isbits:
ref = Ref(1)
m5 = (x = ref, y = ref, z = Ref(1))
Expand Down