# Subtyping vs adding a function to a struct's fields

**URL:** https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638
**Category:** General Usage
**Tags:** question
**Created:** [May 10, 2023, 7:58pm UTC](https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638 "2023-05-10T19:58:16Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [May 12, 2023, 12:21am UTC](https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638/21 "2023-05-12T00:21:15Z")

</div>

I suppose you do not find `map(x -> x.mass, vector_of_b)`? XD

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [May 12, 2023, 1:57am UTC](https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638/22 "2023-05-12T01:57:30Z")

</div>

Awesome, @cstjean. I should also say it’s been many months since I’ve looked, so it’s possible that some of your work maintaining it might have fixed some of the issues. Happy to help diagnosing if you ever get to the point where you become concerned about this.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 12, 2023, 8:30am UTC](https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638/23 "2023-05-12T08:30:09Z")

</div>

Just had a thought if you can guarantee all your box types have the same structure. You could put the box number as a type parameter in a parametric struct definition (defines an iterated union of concrete types). I’m also adding a parameter to make the fields share a floating point type. You can dispatch on the numbered subtypes, and you can just use dot field access instead of interface methods or whatever strategies if you are certain you won’t need to change the structure and fields ever. It’s like moving the separate dispatch argument I suggested earlier into the type parameters for `Box`, which can be neater, but downsides are needing to make an outer constructor and needing to compile for the different `Box{N}` separately even when you don’t need separate methods for different `N`.

```julia
struct Box{N,T}
  material::String
  mass::T
  height::T
  length::T
  width::T
end

# fields cannot imply N, so a Box method is not automatically made
# we can write a Box{N} method
# note that Box{N} is abstract: Box{1} === Box{1,T} where T
Box{N}(a,b::T,c::T,d::T,e::T) where {N,T} = Box{N,T}(a,b,c,d,e)

position(b::Box{1}, t) = (t, t^2)
position(b::Box{2}, t) = (t + b.width/2, t + b.height/2)

position(Box{1}("wood", 5.6, 2.0, 124.0, 4.0), 2.5)
position(Box{2}("iron", 54.8, 2.0, 124.0, 4.0), 2.5)

# function that doesn't have separate methods per N
volume(b::Box) = b.height * b.width * b.length

```

[Previous page](https://discourse.julialang.org/t/subtyping-vs-adding-a-function-to-a-structs-fields/98638.md?page=1)
