diff --git a/src/stack/stack.jl b/src/stack/stack.jl index c1b4ea730..597297627 100644 --- a/src/stack/stack.jl +++ b/src/stack/stack.jl @@ -189,6 +189,10 @@ Base.NamedTuple(s::AbstractDimStack) = NamedTuple(layers(s)) Base.collect(st::AbstractDimStack) = parent([st[D] for D in DimIndices(st)]) Base.Array(st::AbstractDimStack) = collect(st) Base.vec(st::AbstractDimStack) = vec(collect(st)) +Base.get(st::AbstractDimStack, k::Symbol, default) = + haskey(st, k) ? st[k] : default +Base.get(f::Base.Callable, st::AbstractDimStack, k::Symbol) = + haskey(st, k) ? st[k] : f() @propagate_inbounds Base.iterate(st::AbstractDimStack) = iterate(st, 1) @propagate_inbounds Base.iterate(st::AbstractDimStack, i) = i > length(st) ? nothing : (st[DimIndices(st)[i]], i + 1) diff --git a/test/stack.jl b/test/stack.jl index ba5ef205a..10e3e570c 100644 --- a/test/stack.jl +++ b/test/stack.jl @@ -16,7 +16,6 @@ da4 = DimArray(cat(4A, 5A, 6A, 7A; dims=3), (x, y, z); name=:extradim) s = DimStack((da1, da2, da3)) mixed = DimStack(da1, da2, da4) -typeof(s) @testset "constructors" begin @test DimStack((one=A, two=2A, three=3A), dimz) == @@ -67,6 +66,10 @@ end @test eltype(mixed) === @NamedTuple{one::Float64, two::Float32, extradim::Float64} @test haskey(s, :one) == true @test haskey(s, :zero) == false + @test get(mixed, :one, nothing) == mixed.one + @test get(mixed, :five, nothing) == nothing + @test get(() -> nothing, mixed, :two) == mixed.two + @test get(() -> nothing, mixed, :ten) == nothing @test size(mixed) === (2, 3, 4) # size is as for Array @test size(mixed, Y) === 3 @test size(mixed, 3) === 4