A lot of the power and extensibility in Julia comes from a collection of informal interfaces. By extending a few specific methods to work for a custom type, objects of that type not only receive those functionalities, but they are also able to be used in other methods that are written to generically build upon those behaviors.


Required methods Brief description
start(iter) Returns the initial iteration state
next(iter,state) Returns the current item and the next state
done(iter,state) Tests if there are any items remaining
Important optional methodsDefault definitionBrief description
eltype(IterType)AnyThe type the items returned by next()
length(iter)(undefined)The number of items, if known

Sequential iteration is implemented by the methods start(), done(), and next(). Instead of mutating objects as they are iterated over, Julia provides these three methods to keep track of the iteration state externally from the object. The start(iter) method returns the initial state for the iterable object iter. That state gets passed along to done(iter,state), which tests if there are any elements remaining, and next(iter,state), which returns a tuple containing the current element and an updated state. The state object can be anything, and is generally considered to be an implementation detail private to the iterable object.

Any object defines these three methods is iterable and can be used in the many functions that rely upon iteration. It can also be used directly in a for loop since the syntax:

foriiniter# or  "for i = iter"# bodyend

is translated into:

state=start(iter)while!done(iter,state)(i,state)=next(iter,state)# bodyend

A simple example is an iterable sequence of square numbers with a defined length:

julia>immutableSquarescount::IntendBase.start(::Squares),state)=(state*state,state+1)Base.done(S::Squares,s)=s>S.count;Base.eltype(::Type{Squares})=Int# Note that this is defined for the typeBase.length(S::Squares)=S.count;

With only start, next, and done definitions, the Squares type is already pretty powerful. We can iterate over all the elements:


We can use many of the builtin methods that work with iterables, like in(), mean() and std():


There are a few more methods we can extend to give Julia more information about this iterable collection. We know that the elements in a Squares sequence will always be Int. By extending the eltype() method, we can give that information to Julia and help it make more specialized code in the more complicated methods. We also know the number of elements in our sequence, so we can extend length(), too.

Now, when we ask Julia to collect() all the elements into an array it can preallocate a Vector{Int} of the right size instead of blindly push!ing each element into a Vector{Any}:

julia>collect(Squares(100))'# transposed to save space1x100Array{Int64,2}:1491625364964811009025921694099604980110000

While we can rely upon generic implementations, we can also extend specific methods where we know there is a simpler algorithm. For example, there’s a formula to compute the sum of squares, so we can override the generic iterative version with a more performant solution:


This is a very common pattern throughout the Julia standard library: a small set of required methods define an informal interface that enable many fancier behaviors. In some cases, types will want to additionally specialize those extra behaviors when they know a more efficient algorithm can be used in their specific case.


Methods to implementBrief description
getindex(X,i)X[i], indexed element access
setindex!(X,v,i)X[i]=v, indexed assignment
endof(X)The last index, used in X[end]

For the Squares iterable above, we can easily compute the ith element of the sequence by squaring it. We can expose this as an indexing expression S[i]. To opt into this behavior, Squares simply needs to define getindex():

julia>function Base.getindex(S::Squares,i::Int)1<=i<=S.count||throw(BoundsError(S,i))returni*iendSquares(100)[23]529

Additionally, to support the syntax S[end], we must define endof() to specify the last valid index:


Note, though, that the above only defines getindex() with one integer index. Indexing with anything other than an Int will throw a MethodError saying that there was no matching method. In order to support indexing with ranges or vectors of Ints, separate methods must be written:


While this is starting to support more of the indexing operations supported by some of the builtin types, there’s still quite a number of behaviors missing. This Squares sequence is starting to look more and more like a vector as we’ve added behaviors to it. Instead of defining all these behaviors ourselves, we can officially define it as a subtype of an AbstractArray.

Abstract Arrays

Methods to implement Brief description
size(A) Returns a tuple containing the dimensions of A
Base.linearindexing(Type) Returns either Base.LinearFast() or Base.LinearSlow(). See the description below.
getindex(A,i::Int) (if LinearFast) Linear scalar indexing
getindex(A,i1::Int,...,iN::Int) (if LinearSlow, where N=ndims(A)) N-dimensional scalar indexing
setindex!(A,v,i::Int) (if LinearFast) Scalar indexed assignment
setindex!(A,v,i1::Int,...,iN::Int) (if LinearSlow, where N=ndims(A)) N-dimensional scalar indexed assignment
Optional methodsDefault definitionBrief description
getindex(A,I...)defined in terms of scalar getindex()Multidimensional and nonscalar indexing
setindex!(A,I...)defined in terms of scalar setindex!()Multidimensional and nonscalar indexed assignment
start()/next()/done()defined in terms of scalar getindex()Iteration
length(A)prod(size(A))Number of elements
similar(A)similar(A,eltype(A),size(A))Return a mutable array with the same shape and element type
similar(A,::Type{S})similar(A,S,size(A))Return a mutable array with the same shape and the specified element type
similar(A,dims::NTuple{Int})similar(A,eltype(A),dims)Return a mutable array with the same element type and the specified dimensions
similar(A,::Type{S},dims::NTuple{Int})Array(S,dims)Return a mutable array with the specified element type and dimensions

If a type is defined as a subtype of AbstractArray, it inherits a very large set of rich behaviors including iteration and multidimensional indexing built on top of single-element access. See the arrays manual page and standard library section for more supported methods.

A key part in defining an AbstractArray subtype is Base.linearindexing(). Since indexing is such an important part of an array and often occurs in hot loops, it’s important to make both indexing and indexed assignment as efficient as possible. Array data structures are typically defined in one of two ways: either it most efficiently accesses its elements using just one index (linear indexing) or it intrinsically accesses the elements with indices specified for every dimension. These two modalities are identified by Julia as Base.LinearFast() and Base.LinearSlow(). Converting a linear index to multiple indexing subscripts is typically very expensive, so this provides a traits-based mechanism to enable efficient generic code for all array types.

This distinction determines which scalar indexing methods the type must define. LinearFast() arrays are simple: just define getindex(A::ArrayType,i::Int). When the array is subsequently indexed with a multidimensional set of indices, the fallback getindex(A::AbstractArray,I...)() efficiently converts the indices into one linear index and then calls the above method. LinearSlow() arrays, on the other hand, require methods to be defined for each supported dimensionality with ndims(A)Int indices. For example, the builtin SparseMatrix type only supports two dimensions, so it just defines getindex(A::SparseMatrix,i::Int,j::Int)(). The same holds for setindex!().

Returning to the sequence of squares from above, we could instead define it as a subtype of an AbstractArray{Int,1}:


Note that it’s very important to specify the two parameters of the AbstractArray; the first defines the eltype(), and the second defines the ndims(). That supertype and those three methods are all it takes for SquaresVector to be an iterable, indexable, and completely functional array:

julia>s=SquaresVector(7)7-elementSquaresVector:14916253649julia>s[s.>20]3-elementArray{Int64,1}:253649julia>s \ rand(7,2)1x2Array{Float64,2}:0.01518760.0179393

As a more complicated example, let’s define our own toy N-dimensional sparse-like array type built on top of Dict:

julia>immutableSparseArray{T,N}<:AbstractArray{T,N}data::Dict{NTuple{N,Int},T}dims::NTuple{N,Int}endSparseArray{T}(::Type{T},dims::Int...)=SparseArray(T,dims)SparseArray{T,N}(::Type{T},dims::NTuple{N,Int})=SparseArray{T,N}(Dict{NTuple{N,Int},T}(),dims)SparseArray{T,N}julia>Base.size(A::SparseArray)=A.dimsBase.similar{T}(A::SparseArray,::Type{T},dims::Dims)=SparseArray(T,dims)# Define scalar indexing and indexed assignment for up to 3 dimensionsBase.getindex{T}(A::SparseArray{T,1},i1::Int)=get(,(i1,),zero(T))Base.getindex{T}(A::SparseArray{T,2},i1::Int,i2::Int)=get(,(i1,i2),zero(T))Base.getindex{T}(A::SparseArray{T,3},i1::Int,i2::Int,i3::Int)=get(,(i1,i2,i3),zero(T))Base.setindex!{T}(A::SparseArray{T,1},v,i1::Int)=([(i1,)]=v)Base.setindex!{T}(A::SparseArray{T,2},v,i1::Int,i2::Int)=([(i1,i2)]=v)Base.setindex!{T}(A::SparseArray{T,3},v,i1::Int,i2::Int,i3::Int)=([(i1,i2,i3)]=v);

Notice that this is a LinearSlow array, so we must manually define getindex() and setindex!() for each dimensionality we’d like to support. Unlike the SquaresVector, we are able to define setindex!(), and so we can mutate the array:


The result of indexing an AbstractArray can itself be an array (for instance when indexing by a Range). The AbstractArray fallback methods use similar() to allocate an Array of the appropriate size and element type, which is filled in using the basic indexing method described above. However, when implementing an array wrapper you often want the result to be wrapped as well:


In this example it is accomplished by defining Base.similar{T}(A::SparseArray,::Type{T},dims::Dims) to create the appropriate wrapped array. For this to work it’s important that SparseArray is mutable (supports setindex!). similar() is also used to allocate result arrays for arithmetic on AbstractArrays, for instance:


In addition to all the iterable and indexable methods from above, these types can also interact with each other and use all of the methods defined in the standard library for AbstractArrays: