From cde427a5880882015cf66bba0825fb5bc8578441 Mon Sep 17 00:00:00 2001 From: Frederik Baymler Mathiesen Date: Wed, 3 Dec 2025 13:20:42 +0100 Subject: [PATCH 1/3] Modify condition in MultiDimClosure function --- src/StructUtils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StructUtils.jl b/src/StructUtils.jl index ac1039a..45a0923 100644 --- a/src/StructUtils.jl +++ b/src/StructUtils.jl @@ -676,7 +676,7 @@ end function (f::MultiDimClosure{S,A})(i::Int, val) where {S,A} f.dims[f.cur_dim[]] = i - if arraylike(f.style, val) + if arraylike(f.style, val) && f.cur_dim[] > 1 f.cur_dim[] -= 1 st = applyeach(f, f.style, val) f.cur_dim[] += 1 From a16398c6280122972dbcde80a358c5f2521ec202 Mon Sep 17 00:00:00 2001 From: Frederik Baymler Mathiesen Date: Thu, 15 Jan 2026 12:12:26 +0100 Subject: [PATCH 2/3] Add ndims to discover_dims --- src/StructUtils.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/StructUtils.jl b/src/StructUtils.jl index 6b232b3..208d921 100644 --- a/src/StructUtils.jl +++ b/src/StructUtils.jl @@ -168,7 +168,7 @@ initialize(st::StructStyle, T::Type, @nospecialize(source)) = function initialize(st::StructStyle, ::Type{A}, source) where {A<:AbstractArray} if ndims(A) > 1 - dims = discover_dims(st, source) + dims = discover_dims(st, source, ndims(A)) return A(undef, dims) else return A(undef, 0) @@ -660,12 +660,16 @@ end # VERSION < v"1.10" # "[[[1.0],[2.0]]]" => (1, 2, 1) # "[[[1.0,2.0]]]" => (2, 1, 1) # length of innermost array is 1st dim -function discover_dims(style, x) +function discover_dims(style, x, ndims::Int) @assert arraylike(style, x) len = applylength(x) + if ndims == 1 + return (len,) + end + ret = ( applyeach(x) do _, v - return arraylike(style, v) ? EarlyReturn(discover_dims(style, v)) : EarlyReturn(()) + return arraylike(style, v) ? EarlyReturn(discover_dims(style, v, ndims - 1)) : EarlyReturn(()) end )::EarlyReturn return (ret.value..., len) From 629f3e62528f502ca1f03ba65e8c486f62582f2e Mon Sep 17 00:00:00 2001 From: Frederik Baymler Mathiesen Date: Thu, 15 Jan 2026 12:12:31 +0100 Subject: [PATCH 3/3] Add tests --- test/runtests.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 38357df..b84cbac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -288,6 +288,22 @@ end # fill slice [:, :, 2] from second slab [[3,4]] exp3[:, :, 2] = reshape([3,4], 2, 1) @test x3 == exp3 + + # Jagged nested vectors (Vector of Vectors) + jagged = [[1, 2], [3, 4, 5], [6]] + result = StructUtils.make(Vector{Vector{Int}}, jagged) + @test result == jagged + + # Multi-dimensional case with jagged inner vectors + vv_jagged = [[[1, 2], [3]], [[4, 5, 6], [7, 8]]] + result_md = StructUtils.make(Array{Vector{Int}, 2}, vv_jagged) + + md_jagged = Array{Vector{Int}}(undef, 2, 2) + md_jagged[1, 1] = [1, 2] + md_jagged[2, 1] = [3] + md_jagged[1, 2] = [4, 5, 6] + md_jagged[2, 2] = [7, 8] + @test result_md == md_jagged end @testset "@nonstruct macro" begin