# Prepend! usage / what methods to implement?

**URL:** <https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232>\
**Category:** General Usage\
**Created:** [February 21, 2018, 2:58pm UTC](https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232 "2018-02-21T14:58:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [February 21, 2018, 2:58pm UTC](https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232/1 "2018-02-21T14:58:24Z")

</div>

Hi,

What methods should i be implementing if i am to use prepend! on a user defined datatype? Can you show me an example?  
Thanks

```julia
julia> struct R
           x::Int
       end
julia> function f(r::R)
           linearized = R[]
           prepend!(linearized,r)
           linearized
       end
f (generic function with 1 method)

julia> f(R(3))
ERROR: MethodError: no method matching length(::R)
Closest candidates are:
  length(::SimpleVector) at essentials.jl:256
  length(::Base.MethodList) at reflection.jl:558
  length(::MethodTable) at reflection.jl:634
  ...
Stacktrace:
 [1] _prepend!(::Array{R,1}, ::Base.HasLength, ::R) at ./array.jl:722
 [2] f(::R) at ./REPL[7]:3

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 21, 2018, 3:53pm UTC](https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232/2 "2018-02-21T15:53:30Z")

</div>

A custom type with no parent type basically doesn’t know how to do anything. Try implementing the `length` method and then see what fails after you implement that. When the code stops failing, you’ve implemented enough methods. What data structure are you trying to implement?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 21, 2018, 5:13pm UTC](https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232/3 "2018-02-21T17:13:45Z")

</div>

I have a hunch that you actually want to be calling `unshift!(linearized,r)` (it has a better name on 0.7: `pushfirst!`).

`prepend!` and `append!` tack two collections together. `push!` and `unshift!` add a single element to a collection.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 21, 2018, 5:46pm UTC](https://discourse.julialang.org/t/prepend-usage-what-methods-to-implement/9232/4 "2018-02-21T17:46:25Z")

</div>

Ah yes, that does seem likely. I (probably) took the question too literally.
