# How do I define my own type based on Julia's pre-existing type?

**URL:** <https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835>\
**Category:** General Usage\
**Tags:** type, interface\
**Created:** [January 31, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835 "2023-01-31T18:22:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 31, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835/1 "2023-01-31T18:22:32Z")

</div>

Hello!

I am trying to perform a simple experiment and need some help.

I wish to make a copy of the AbstractVector type as such:

```julia
struct AbstractVectorCopy{T} <: AbstractVector{T}
    field::AbstractVector{T} 
end

```

Then I want to be able to fill it with values, exactly the same way as for the AbstractVector:

```julia
using StaticArrays
# This works using AbstractVector
AbstractVector{SVector{3,Float32}}(zeros(SVector{3,Float32},100))

# This does not work using the copy
AbstractVectorCopy{SVector{3,Float32}}(zeros(SVector{3,Float32},100))

```

Any way to make it properly act like an “AbstractVector” but through my “new” type?

Kind regards

---

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [January 31, 2023, 6:28pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835/2 "2023-01-31T18:28:04Z")

</div>

Hi,

have you looked into the docs at this page: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/)

The interface for arrays is described here: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)

When you implement these interfaces, your struct should be usable everywhere where `AbstractVector` is supported.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 31, 2023, 6:46pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835/3 "2023-01-31T18:46:21Z")

</div>

No I had not seen that, thank you!

Very unclear to me a lot of things, but I think I got the basics:

```julia
Base.size(S::AbstractVectorCopy) = (length(S.field),)
Base.IndexStyle(::Type{<:AbstractVectorCopy}) = IndexLinear()
Base.getindex(S::AbstractVectorCopy, i::Int) = S.field[i]

AbstractVectorCopy{SVector{3,Float32}}(zeros(SVector{3,Float32},100))

```

So that is a start, since that now works! 🙂

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 31, 2023, 7:09pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835/4 "2023-01-31T19:09:47Z")

</div>

[https://docs.juliahub.com/Lazy/mi7Ul/0.15.0/autodocs/#Lazy.@forward-Tuple{Any,Any}](https://docs.juliahub.com/Lazy/mi7Ul/0.15.0/autodocs/#Lazy.@forward-Tuple%7BAny,Any%7D)

may come handy if you just want a wrapper

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 31, 2023, 8:54pm UTC](https://discourse.julialang.org/t/how-do-i-define-my-own-type-based-on-julias-pre-existing-type/93835/5 "2023-01-31T20:54:49Z")

</div>

> [@Ahmed\_Salih](#):
>
> I wish to make a copy of the AbstractVector type as such:
> 
> ```julia
> struct AbstractVectorCopy{T} <: AbstractVector{T}
> field::AbstractVector{T} 
> end
> 
> ```

Have you seen performance tips, the “Type declarations” section in particular: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Type-declarations)

As explained in the first two subsections, “Avoid fields with abstract type” and “Avoid fields with abstract containers”, you probably don’t want to declare your `field` with an abstract type, for performance reasons.
