We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
@functor
1 parent 75fef99 commit 7c87d42Copy full SHA for 7c87d42
src/functor.jl
@@ -16,7 +16,7 @@ function makefunctor(m::Module, T, fs = fieldnames(T))
16
escfs = [:($f = getfield(x, $(QuoteNode(f)))) for f in fs]
17
18
@eval m begin
19
- $Functors.functor(::Type{<:$T}, x) = ($(escfs...),), y -> $T($(escargs...))
+ $Functors.functor(::Type{<:$T}, x) = (;$(escfs...)), y -> $T($(escargs...))
20
end
21
22
@@ -30,7 +30,7 @@ macro functor(args...)
30
functorm(args...)
31
32
33
-isleaf(x) = children(x) === ()
+isleaf(@nospecialize(x)) = children(x) === ()
34
35
children(x) = functor(x)[1]
36
test/basics.jl
@@ -19,6 +19,15 @@ struct NoChild{T}; x::T; end
### Basic functionality
###
+@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
+
@testset "Nested" begin
model = Bar(Foo(1, [1, 2, 3]))
@@ -44,7 +53,7 @@ end
44
53
@testset "Property list" begin
45
54
model = OneChild3(1, 2, 3)
46
55
model′ = fmap(x -> 2x, model)
47
-
56
48
57
@test (model′.x, model′.y, model′.z) == (1, 4, 3)
49
58
50
59
@@ -77,7 +86,7 @@ end
77
86
m4 = (x=1, y=(2, 3), z=4:5)
78
87
@test isbits(fmap(float, m4))
79
88
@test_skip 0 == @allocated fmap(float, m4) # true, but fails in tests
80
89
81
90
# Shared mutable containers are preserved, even if all children are isbits:
82
91
ref = Ref(1)
83
92
m5 = (x = ref, y = ref, z = Ref(1))
0 commit comments