From 6d6d7650ae555d019617691bb52e74499e412f6d Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Thu, 23 Nov 2023 14:52:55 +0100 Subject: [PATCH] add `AutoVal` struct so keywords can be incorporated later --- src/Dimensions/Dimensions.jl | 2 +- src/Dimensions/dimension.jl | 20 +++++++++++++++++++- src/LookupArrays/lookup_arrays.jl | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Dimensions/Dimensions.jl b/src/Dimensions/Dimensions.jl index 21a08adb9..95264276f 100644 --- a/src/Dimensions/Dimensions.jl +++ b/src/Dimensions/Dimensions.jl @@ -35,7 +35,7 @@ export name, label, dimnum, hasdim, hasselection, otherdims, commondims, combine basetypeof, basedims, dimstride, dims2indices, slicedims, dimsmatch, comparedims, reducedims export Dimension, IndependentDim, DependentDim, XDim, YDim, ZDim, TimeDim, - X, Y, Z, Ti, Dim, AnonDim, Coord, MergedLookup + X, Y, Z, Ti, Dim, AnonDim, Coord, MergedLookup, AutoVal export @dim diff --git a/src/Dimensions/dimension.jl b/src/Dimensions/dimension.jl index 3c6d9ce93..e45ea4648 100644 --- a/src/Dimensions/dimension.jl +++ b/src/Dimensions/dimension.jl @@ -148,6 +148,12 @@ const AllDims = Union{Symbol,Dimension,DimTuple,SymbolTuple,DimType,DimTypeTuple # DimensionalData interface methods +struct AutoVal{T,K} + val::T + kw::K +end +val(av::AutoVal) = av.val + """ rebuild(dim::Dimension, val) => Dimension rebuild(dim::Dimension; val=val(dim)) => Dimension @@ -329,8 +335,13 @@ Dim{:custom} Char['a', 'b', 'c'] """ struct Dim{S,T} <: Dimension{T} val::T + function Dim{S}(val; kw...) where {S} + if length(kw) > 0 + val = AutoVal(val, values(kw)) + end + new{S,typeof(val)}(val) + end end -Dim{S}(val::T) where {S,T} = Dim{S,T}(val) function Dim{S}(val::AbstractArray; kw...) where S if length(kw) > 0 val = AutoLookup(val, values(kw)) @@ -393,6 +404,13 @@ function dimmacro(typ, supertype, name::String=string(typ)) quote Base.@__doc__ struct $typ{T} <: $supertype{T} val::T + function $typ(val; kw...) + if length(kw) > 0 + val = AutoVal(val, values(kw)) + end + new{typeof(val)}(val) + end + $typ{T}(val::T; kw...) where T = new(val::T) end function $typ(val::AbstractArray; kw...) if length(kw) > 0 diff --git a/src/LookupArrays/lookup_arrays.jl b/src/LookupArrays/lookup_arrays.jl index ded3f9e85..78ca0c6a9 100644 --- a/src/LookupArrays/lookup_arrays.jl +++ b/src/LookupArrays/lookup_arrays.jl @@ -423,7 +423,7 @@ _check_ordered_cyclic(order::Unordered) = throw(ArgumentError("Cyclic lookups mu function rebuild(l::Cyclic; data=parent(l), order=order(l), span=span(l), sampling=sampling(l), metadata=metadata(l), - cycle=cycle(l), _cycle_status=cycle_status(l), kw... + cycle=cycle(l), cycle_status=cycle_status(l), kw... ) Cyclic(data, order, span, sampling, metadata, cycle, cycle_status) end