# Implement AbstractArray so that custom type is returned

**URL:** <https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 15, 2021, 4:26am UTC](https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297 "2021-02-15T04:26:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kblissett](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@kblissett](https://discourse.julialang.org/u/kblissett)\
**Post date:** [February 15, 2021, 4:26am UTC](https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297/1 "2021-02-15T04:26:49Z")

</div>

I’d like to implement a 3-D vector type like the following:

```julia
struct Vector3D
    x::Int
    y::Int
    z::Int
end

```

Julia has a rich collection of Array operations already defined that I’d love to take advantage of, so I’ll make my Vector a subtype of `AbstractVector{Int}`

```julia
struct Vector3D <: AbstractVector{Int}
    x::Int
    y::Int
    z::Int
end

Base.size(::Vector3D) = (3,)
Base.getindex(v::Vector3D, i::Integer) = i == 1 ? v.x : i == 2 ? v.y : v.z

```

But when I perform the following operation, an `Array` type is returned rather than my `Vector3D` type.

```julia
julia> typeof(Vector3D(1, 2, 3) .+ 4)
Array{Int64,1}

```

What do I need to do so that `Vector3D(1, 2, 3) .+ 4` returns a `Vector3D` type instead?

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [February 15, 2021, 5:55am UTC](https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297/2 "2021-02-15T05:55:34Z")

</div>

> [@kblissett](#):
>
> What do I need to do so that `Vector3D(1, 2, 3) .+ 4` returns a `Vector3D` type instead?

You need to implement the [broadcast interface](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting) for `Vector3D`. I always find that interface to be a bit of a pain, and the most reliable way is to start with the `ArrayAndChar` example and modify it one step at a time.

Good luck!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 15, 2021, 6:44am UTC](https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297/3 "2021-02-15T06:44:15Z")

</div>

Unless you are doing this for your own education you should check out [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl), where all of this is already implemented.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 15, 2021, 8:20am UTC](https://discourse.julialang.org/t/implement-abstractarray-so-that-custom-type-is-returned/55297/4 "2021-02-15T08:20:20Z")

</div>

To expand on the `StaticArrays` suggestion, here is an example:

```julia
julia> using StaticArrays

julia> struct Vector3D <: FieldVector{3,Int}
       x :: Int
       y :: Int
       z :: Int
       end

julia> Vector3D(1,2,3)
3-element Vector3D with indices SOneTo(3):
 1
 2
 3

julia> Vector3D(1,2,3) .+ 1
3-element Vector3D with indices SOneTo(3):
 2
 3
 4

```
