# How to enforce the fields to be inherited?

**URL:** https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693
**Category:** New to Julia
**Tags:** inheritance
**Created:** [February 12, 2019, 5:03am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693 "2019-02-12T05:03:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 12, 2019, 5:03am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/1 "2019-02-12T05:03:42Z")

</div>

hello, coming across `StatsBase.AbstractWeights`, I found it **could enforce fields to be inherited**. e.g.

```julia
import StatsBase

struct MyWeights1{S<:Real, T<:Real, V<:AbstractVector{T}} <: StatsBase.AbstractWeights{S, T, V}
    values::V
    sum::S
end

struct MyWeights2{S<:Real, T<:Real, V<:AbstractVector{T}} <: StatsBase.AbstractWeights{S, T, V}
    notvalues::V
    notsum::S
end

x = MyWeights1([1.0, 2.0], 3.0)
y = MyWeights2([1.0, 2.0], 3.0)

julia> x = MyWeights1([1.0, 2.0], 3.0)
2-element MyWeights1{Float64,Float64,Array{Float64,1}}:
 1.0
 2.0

julia> y = MyWeights2([1.0, 2.0], 3.0)
Error showing value of type MyWeights2{Float64,Float64,Array{Float64,1}}:
ERROR: type MyWeights2 has no field values

```

I tried to go thru the [code](https://github.com/JuliaStats/StatsBase.jl/blob/88b481809cf3a4b4e381be37f4372122c2d7c361/src/weights.jl#L86-L96), but not understand how could it be done 😓

could someone be nice enough to explain the trick? thanks.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 12, 2019, 6:45am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/2 "2019-02-12T06:45:11Z")

</div>

I don’t know the code but I assume that it just requires those fields to be defined for the methods to work. Have a look at show method which threw above error: `@less show(stdout, MyWeights2([1.0, 2.0], 3.0))`. I suspect it is defined on the AbstractWeights and accesses `.value`.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 12, 2019, 9:32am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/3 "2019-02-12T09:32:49Z")

</div>

oic! it does **_not_** enforce the field to inherit at all! The following call:

```julia
y = MyWeights2([1.0, 2.0], 3.0)

```

would make the REPL to `show()` the object, which in turn calls:

```julia
Base.size(wv::AbstractWeights) = size(wv.values)

```

that triggers an error.

Instead, if we suppress REPL from `show()`ing the object like:

```julia
julia> z = MyWeights2([1.0, 2.0], 3.0);

julia> fieldnames(MyWeights1)
(:values, :sum)

julia> fieldnames(MyWeights2)
(:notvalues, :notsum)

```

any field would be allowed: no enforcement of the fields to be inherited 🙁

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 12, 2019, 9:42am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/4 "2019-02-12T09:42:16Z")

</div>

Yes, that works. But presumably other functionality of StatsBase doesn’t work either with those fields. It should be described in the docs, what needs to be satisfied to hook into the functionality of StatsBase.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 12, 2019, 10:18am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/5 "2019-02-12T10:18:39Z")

</div>

actuall, it’s **disappointing** : seems like there’s no easy way to enforce the fields to be inherited.

any suggestions?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 12, 2019, 10:26am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/6 "2019-02-12T10:26:01Z")

</div>

Yep. Interfaces in Julia are informal. There are some packages which help with inheriting fields, there was an announcement recently on discourse.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 12, 2019, 10:32am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/7 "2019-02-12T10:32:25Z")

</div>

> [@mauro3](#):
>
> , there was an announcement recently on discourse.

could you please give a link?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 12, 2019, 10:39am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/8 "2019-02-12T10:39:35Z")

</div>

> **[GitHub - gcalderone/ReusePatterns.jl: Implement composition and concrete...](https://github.com/gcalderone/ReusePatterns.jl)**
>
> Implement composition and concrete subtyping in Julia. - GitHub - gcalderone/ReusePatterns.jl: Implement composition and concrete subtyping in Julia.

> [@Why is it impossible to subtype a struct?](https://discourse.julialang.org/t/why-is-it-impossible-to-subtype-a-struct/19876):
>
> Is there a deeper reason, why the following does not work? I guess a struct can only be a leaf in the type hierarchy. Is there a reason why this has to be like this? julia\> struct TypeA{T} data::T end julia\> struct TypeB{T} \<: TypeA{T} data::T end ERROR: invalid subtyping in definition of TypeB Edit, I found the follwing in the [Manual](https://docs.julialang.org/en/v1/manual/types/): One particularly distinctive feature of Julia’s type system is that concrete types may not subtype each other: all co…

---

<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: [February 12, 2019, 10:45am UTC](https://discourse.julialang.org/t/how-to-enforce-the-fields-to-be-inherited/20693/9 "2019-02-12T10:45:36Z")

</div>

> [@tomtom](#):
>
> could someone be nice enough to explain the trick? thanks.

There is no trick. As @mauro3 explained, some code just assumes that when `typeof(x) <: AbstractWeights`, it has certain fields implemented.

It is not clear that `StatsBase.AbstractWeights` was meant to be extended outside the package. However, if you want to do this, and implement it with different fields, you should just implement the interface, which is [probably something like this](https://github.com/JuliaStats/StatsBase.jl/blob/6ce5225310f92b6c9f1ad221deafae93fd5b13d5/src/weights.jl#L20-L24).
