Julia does not allow inherit concrete types. So how should I implement my type using existing concrete type? For example, I want to implement MyArray
with AxisArrays. AxisArray
, they share the same methods, the only difference is that MyArray
has some constraints on its constructor, so it is easier for the users to write their code.
For example, I can wrap AxisArray
in MyArray
,
using AxisArrays
struct MyArray
a::AxisArray
function MyArray(A)
size(A) > (3, 4) && error()
new(A)
end
end
and forward every method of AxisArray
to MyArray
(using @forward
in MacroTools.jl). This is the composition way.
On the other hand, I could just make an alias to AxisArray
:
function MyArray(a...)
size(a[1]) > (3, 4) && error()
AxisArray(a...)
end
julia> MyArray(rand(2,3), (Axis{:a}([1,2]), Axis{:b}([1,2,3])))
2×3 AxisArray{Float64,2,Array{Float64,2},Tuple{Axis{:a,Array{Int64,1}},Axis{:b,Array{Int64,1}}}}:
0.233249 0.549376 0.399034
0.208456 0.2461 0.240907
But want it returns is an AxisArray
. How to change it to be shown as MyArray
also? Just modify AxisArray
’s show
method?
Which way is recommended in the above 2?