# How to wrap a concrete type in a user-defined type?

**URL:** <https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491>\
**Category:** General Usage\
**Tags:** question, type\
**Created:** [February 6, 2019, 3:08am UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491 "2019-02-06T03:08:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [February 6, 2019, 3:08am UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491/1 "2019-02-06T03:08:23Z")

</div>

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`,

```julia
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](https://github.com/MikeInnes/MacroTools.jl)). This is the composition way.

On the other hand, I could just make an alias to `AxisArray`:

```julia
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?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 6, 2019, 5:35am UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491/2 "2019-02-06T05:35:22Z")

</div>

Modifying `show` is ultimately unhelpful as you will want to dispatch using your type’s typename (sooner or later).

`@forward` whatever you should and that way you are open to future

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 6, 2019, 7:31am UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491/3 "2019-02-06T07:31:37Z")

</div>

I think option 2 is good, but why do you want it to display as `MyArray`? Will you dispatch on this type? If not, I would just call the method that does the size checking something like `create_array` or `verify_array` and not pretend that it was its own type.

If you need your own type, then you can either forward the methods, or alternatively, just refer to the field `myarray.a` directly.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [February 6, 2019, 12:29pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491/4 "2019-02-06T12:29:12Z")

</div>

It depends on what your goal is. If you want to dispatch on the specialized type, then you need to make it a separate type, if all you want is to check a parameter on construction, then your second option might be better.

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [February 7, 2019, 9:08am UTC](https://discourse.julialang.org/t/how-to-wrap-a-concrete-type-in-a-user-defined-type/20491/5 "2019-02-07T09:08:45Z")

</div>

> [@singularitti](#):
>
> modify `AxisArray` 's `show` method?

This looks like [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/index.html#Avoid-type-piracy-1), although harmless.

You can forward all the calls from `MyArray` to `AxisArrays` with [ReusePatterns](https://github.com/gcalderone/ReusePatterns.jl), and define your own `show` method as follows:

```julia
julia> using ReusePatterns, AxisArrays

julia> struct MyArray
              a::AxisArray
              function MyArray(A)
                  size(A) > (3, 4) && error()
                  new(A)
              end
       end

julia> @forward (MyArray, :a) AxisArray

julia> Base.show(a::MyArray) = "This is MyArray!"

```
