Skip to content

Commit 3b45274

Browse files
authored
Merge pull request #36 from FluxML/bc/macro-empty-namedtuple
Return empty NamedTuple in `@functor` instead of empty Tuple
2 parents 04d08aa + 7c87d42 commit 3b45274

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/functor.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function makefunctor(m::Module, T, fs = fieldnames(T))
1616
escfs = [:($f = getfield(x, $(QuoteNode(f)))) for f in fs]
1717

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

@@ -30,7 +30,7 @@ macro functor(args...)
3030
functorm(args...)
3131
end
3232

33-
isleaf(x) = children(x) === ()
33+
isleaf(@nospecialize(x)) = children(x) === ()
3434

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

test/basics.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ struct NoChild{T}; x::T; end
1919
### Basic functionality
2020
###
2121

22+
@testset "Children and Leaves" begin
23+
no_children = NoChildren2(1, 2)
24+
has_children = Foo(1, 2)
25+
@test Functors.isleaf(no_children)
26+
@test !Functors.isleaf(has_children)
27+
@test Functors.children(no_children) == ()
28+
@test Functors.children(has_children) == (x=1, y=2)
29+
end
30+
2231
@testset "Nested" begin
2332
model = Bar(Foo(1, [1, 2, 3]))
2433

@@ -44,7 +53,7 @@ end
4453
@testset "Property list" begin
4554
model = OneChild3(1, 2, 3)
4655
model′ = fmap(x -> 2x, model)
47-
56+
4857
@test (model′.x, model′.y, model′.z) == (1, 4, 3)
4958
end
5059

@@ -77,7 +86,7 @@ end
7786
m4 = (x=1, y=(2, 3), z=4:5)
7887
@test isbits(fmap(float, m4))
7988
@test_skip 0 == @allocated fmap(float, m4) # true, but fails in tests
80-
89+
8190
# Shared mutable containers are preserved, even if all children are isbits:
8291
ref = Ref(1)
8392
m5 = (x = ref, y = ref, z = Ref(1))

0 commit comments

Comments
 (0)