# How to actually replace OO structures in Julia?

**URL:** https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198
**Category:** New to Julia
**Tags:** struct
**Created:** [November 9, 2021, 10:05am UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198 "2021-11-09T10:05:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [November 9, 2021, 10:05am UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/1 "2021-11-09T10:05:49Z")

</div>

Hi,

I need your help with the following common task which, when described using classes looks like below.

We start with a reference or idealized model.  
Later, the model is going to be refined, properties and methods get modified, replaced, or added.

How to best implement something like this in Julia without OO objects, inheritance, and overloading?

```julia
class ref
	prop1
	prop2
	meth1 = f1ref // of many
	meth2 = f2ref
end

class mod1 // (of some)
	prop1
	prop2
	propnew
	meth1 = f1ref
	meth2 = f2mod
	meth3 = f3new
end

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 9, 2021, 10:34am UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/2 "2021-11-09T10:34:23Z")

</div>

Naively, I’d write something like this:

```julia
struct ref
    prop1
    prop2
end

meth1(obj::ref) = f1ref()
meth2(obj::ref) = f2ref()

struct mod1
    prop1
    prop2
    propnew
end

meth1(obj::mod1) = f1ref()
meth2(obj::mod1) = f2mod()
meth3(obj::mod1) = f3new()

```

Note that I have ignored the convention to uppercase type names here and left the fields of the struct untyped (which will be bad for performance).

If you have lots of shared fields `prop1` … `propN`, you might want to group them in a separate struct, say,

```julia
struct SharedProperties
    prop1
    prop2
    ....
end

```

If you really want to mimick some form of structural inheritance, there a packages that give you something like it, e.g. [Classes.jl](https://github.com/rjplevin/Classes.jl) or [StructuralInheritance.jl](https://github.com/WschW/StructuralInheritance.jl). But, really, you should think about whether you actually need to inherit (parts of) the data structure. In Julia, we generally design type hierarchies by inheriting behavior rather than structure.

Let me also point you to an old Julia resource by Chris on this topic that I liked reading back in 2017: [Type-Dispatch Design: Post Object-Oriented Programming for Julia - Stochastic Lifestyle](https://www.stochasticlifestyle.com/type-dispatch-design-post-object-oriented-programming-julia/)

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [November 9, 2021, 11:03am UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/3 "2021-11-09T11:03:13Z")

</div>

Thx @carstenbauer

In my case, the naive approach would require a lot of manual code repetition.

Can dispatch be used to define “default” methods?  
Would reduce typing as well.

I’m afraid I will need some sort of OO mimicking.  
ref, mod1 and the others cannot be simply abstracted away but have their own “personality”.  
In real life they represent different hardware implementations to be judged for their performance.

The objects can be described by a common configuration file, does this help?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 9, 2021, 11:12am UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/4 "2021-11-09T11:12:52Z")

</div>

> [@Bardo](#):
>
> Can dispatch be used to define “default” methods?

Julia (through multiple dispatch) always chooses the most specific method of a generic function. So you can readily define a default for, say, `meth1` like so

```julia
meth1(obj) = default() # not type annotation in the function signature

```

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [November 9, 2021, 3:19pm UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/5 "2021-11-09T15:19:25Z")

</div>

> [@carstenbauer](#):
>
> packages that give you something like it, e.g. [Classes.jl](https://github.com/rjplevin/Classes.jl) or [StructuralInheritance.jl](https://github.com/WschW/StructuralInheritance.jl)

Yes, looks indeed helpful, was not aware of these, thx!  
I was also looking further along

> The objects can be described by a common configuration file, does this help?

and found [Model configuration/parameterization file - General Usage - JuliaLang](https://discourse.julialang.org/t/model-configuration-parameterization-file/8982).  
The suggested flow with [Parameters.jl](https://github.com/mauro3/Parameters.jl) could do without objects.

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [November 9, 2021, 5:35pm UTC](https://discourse.julialang.org/t/how-to-actually-replace-oo-structures-in-julia/71198/6 "2021-11-09T17:35:03Z")

</div>

Here’s a simple, idiomatic example of how dispatch is used to define default methods (adapted from [this talk](https://www.youtube.com/watch?v=kc9HwsxE1OY)):

```julia
abstract type Animal end
speak(x::Animal) = "Some noise"

struct Dog <: Animal end
speak(x::Dog) = "Woof"
struct Cat <: Animal end
speak(x::Cat) = "Meow"
struct Snake <: Animal end

speak(Dog()) # "Woof"
Speak(Cat()) # "Meow"
speak(Snake()) # "Some noise"

```

`Dog` and `Cat` types have a specific `speak` method which gets called, but `Snake` doesn’t, so it falls back to the more generic method for abstract `Animal`s.
