Skip to content

Commit 15a723a

Browse files
authored
Merge pull request #226 from piever/pv/emptytuple
support empty tuples and named tuples
2 parents 1db687e + a492500 commit 15a723a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/to_vec.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ end
227227

228228
# Non-array data structures
229229

230+
function to_vec(::Tuple{})
231+
vec = Bool[]
232+
function Tuple_from_vec(_)
233+
return ()
234+
end
235+
return vec, Tuple_from_vec
236+
end
237+
230238
function to_vec(x::Tuple)
231239
x_vecs_and_backs = map(to_vec, x)
232240
x_vecs, x_backs = first.(x_vecs_and_backs), last.(x_vecs_and_backs)

test/to_vec.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ end
226226
x_comp = Tangent{typeof(x_outer)}(1, Tangent{typeof(x_inner)}(2, 3))
227227
test_to_vec(x_comp; check_inferred=false)
228228
end
229+
230+
@testset "empty" begin
231+
x_tup = ()
232+
x_comp = Tangent{typeof(x_tup)}(x_tup...)
233+
test_to_vec(x_comp)
234+
end
229235
end
230236

231237
@testset "Tangent Struct" begin
@@ -235,6 +241,12 @@ end
235241
test_to_vec(comp)
236242
end
237243

244+
@testset "NamedTuple empty" begin
245+
nt = NamedTuple()
246+
comp = Tangent{typeof(nt)}(; nt...)
247+
test_to_vec(comp)
248+
end
249+
238250
@testset "Struct" begin
239251
test_to_vec(Tangent{ThreeFields}(; a=10.0, b=20.0, c=30.0))
240252
test_to_vec(Tangent{ThreeFields}(; a=10.0, b=20.0,)) # broken on Julia 1.6.0, fixed on 1.6.1

0 commit comments

Comments
 (0)