How do you inherit Interface?



How do you inherit interfaces in Julia? There is some required functions for certain types, like subtypes of AbstractArray, etc.

Will it be possible to inherit those required functions that is not actually different from a new custom type? e.g

mutable struct MyArray{T, N} <: AbstractArray{T, N}
    data::Array{T, N}

There is quite a lot required functions for AbstractArray, therefore, I have to manually write something like

import Base: length
length(x::MyArray) = length(

Will it be possible to only edit what is different? or just inherit the whole interface.


There is no generic built-in mechanism for this. For the special (and quite common) case when you want the container to “inherit” functionality from an element, some packages have macros that implement this, eg Lazy.@forward.

Help with my string type

I think it’s also worth mentioning that a bare bones implementation of something that wraps an array with some extra data can be quite succint: e.g.

struct MyCustomArray{T,N} <: AbstractArray{T,N}
Base.size(x::MyCustomArray) = size(
@inline @Base.propagate_inbounds Base.getindex(x::MyCustomArray,i::Int) = getindex(,i)
@inline @Base.propagate_inbounds Base.setindex!(x::MyCustomArray,v,i::Int) = setindex(,v,i)
Base.IndexStyle(x::MyCustomArray) = IndexLinear()
Base.similar(x::MyCustomArray,::Type{S},dims::NTuple{Int}) = MyCustomArray(similar(x,S,dims),

I believe that should make a relatively performant custom array type (haven’t tested it at all so there might be errors). Most of the AbstractArray methods have default methods that will fallback to these definitions (e.g. length calls size by default).

Though, until you use v0.7 and implement broadcast_similar and friends broadcasting the custom array type will result in an Array rather than a MyCustomArray.


Oh, I forgot to mention you can find information about the minimal set of methods you need to override in the Interfaces section of the manual.


This lack of inheritance is a disappointing aspect of Julia for me, though I understand its reasoning with multiple dispatch and I’ll prefer multiple dispatch anyday.

For example I need to extend StatsBase.Histogram to include some fields like min, max, sum, and it just requires the creation of several pass-through functions. I’d say it’d be nice to get them for free, but nothing in Java or C++ is for free, and I’d have the same problems with R.

If anyone does know of a drop-in replacement for StatsBase.Histogram then please chime in!


This macro is super-cool!

Just sharing -


The Package Lazy.jl looks cool! Thanks. Does this forward has any overhead comparing to direct overloading?

I also remember there was an issue to discuss the interface feature for 2.0+, but I cannot find it anymore. Do you know which issue is it?


I actually remember there was an issue about interface inheriting but I cannot find it anymore… Hope this would be solved in future Julia version.


I don’t see why it should, the macro just expands to code one would write.