# How do you inherit Interface?

**URL:** https://discourse.julialang.org/t/how-do-you-inherit-interface/10097
**Category:** General Usage
**Tags:** question
**Created:** [April 1, 2018, 7:15am UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097 "2018-04-01T07:15:15Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [April 1, 2018, 7:15am UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/1 "2018-04-01T07:15:15Z")

</div>

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

```julia
mutable struct MyArray{T, N} <: AbstractArray{T, N}
    data::Array{T, N}
    name::String
end

```

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

```julia
import Base: length
length(x::MyArray) = length(x.data)

```

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

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 1, 2018, 7:42am UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/2 "2018-04-01T07:42:50Z")

</div>

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`](https://github.com/MikeInnes/Lazy.jl/blob/76dab7d30863e8375fc96b2444fdb2ead369bf4c/src/macros.jl#L248-L271).

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [April 1, 2018, 5:51pm UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/3 "2018-04-01T17:51:36Z")

</div>

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.

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

```

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`.

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [April 1, 2018, 7:54pm UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/4 "2018-04-01T19:54:54Z")

</div>

Oh, I forgot to mention you can find information about the minimal set of methods you need to override in the [Interfaces](https://docs.julialang.org/en/stable/manual/interfaces/) section of the manual.

---

<div class="post-metadata">

### Author: ![pasha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pasha/32/3319_2.png) [@pasha](https://discourse.julialang.org/u/pasha)
#### Post date: [April 1, 2018, 9:42pm UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/5 "2018-04-01T21:42:16Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [April 1, 2018, 10:00pm UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/6 "2018-04-01T22:00:23Z")

</div>

> [@Tamas\_Papp](#):
>
> some packages have macros that implement this, eg Lazy.@forward

This macro is super-cool!

Just sharing - [https://github.com/tk3369/julia-notebooks/blob/master/Lazy%20%40forward%20macro.ipynb](https://github.com/tk3369/julia-notebooks/blob/master/Lazy%20%40forward%20macro.ipynb).

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [April 2, 2018, 8:24am UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/7 "2018-04-02T08:24:52Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)
#### Post date: [April 2, 2018, 8:25am UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/8 "2018-04-02T08:25:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 2, 2018, 1:57pm UTC](https://discourse.julialang.org/t/how-do-you-inherit-interface/10097/9 "2018-04-02T13:57:17Z")

</div>

> [@Roger-luo](#):
>
> Does this forward has any overhead comparing to direct overloading?

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