jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
286 stars 34 forks source link

The ComponentArray type that can specify the properties of the ComponentArray object #247

Closed chooron closed 4 months ago

chooron commented 4 months ago

Hello, the title might be confusing, but I will explain the issue in detail. I am building a function that requires me to specify the data type it accepts, as shown below:

function demo(input::ComponentVector{Float64, Vector{Float64}, Tuple{Axis{(a = 1, b = 2)}}})
    ...
end

In this function, I want to input a variable input that can be both ComponentVector(a=1,b=2) and ComponentVector(a=ones(100),b=ones(100)). However, the constructed function can only accept ComponentVector(a=1,b=2) because the type of ComponentVector(a=ones(100),b=ones(100)) is ComponentVector{Float64, Vector{Float64}, Tuple{Axis{(a = 1:100, b = 101:200)}}}.

Currently, I have a workaround, which is to first convert the ComponentVector to a @NamedTuple type, and the function accepts parameters as:

Union{NamedTuple{(a, b), NTuple{2, Float64}}, NamedTuple{(a, b), NTuple{2, Vector{Float64}}}}

But converting ComponentVector to @NamedTuple type consumes a lot of computational resources (about 10 times more).