# Trait inheritance

**URL:** https://discourse.julialang.org/t/trait-inheritance/21021
**Category:** General Usage
**Tags:** traits
**Created:** [February 20, 2019, 7:12pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021 "2019-02-20T19:12:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)
#### Post date: [February 20, 2019, 7:12pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021/1 "2019-02-20T19:12:38Z")

</div>

Is it possible to dispatch on a trait inheritance hierarchy without invoking dynamic dispatch?

---

<div class="post-metadata">

### Author: ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)
#### Post date: [February 20, 2019, 7:59pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021/2 "2019-02-20T19:59:31Z")

</div>

if dispatch can be determined at compile time it will be for example:

```julia
struct SomeType
       x::Int 
end

struct SomeTrait end

hasSomeTrait(::SomeType) = SomeTrait()
hasSomeTrait(::Any) = nothing

f(x) = f(x,hasSomeTrait(x))
f(x,::SomeTrait) = 3
f(x,::Any) = 4

```

If we look at the code produced we can see that the compiler used static dispatch. It can do this because of constant propagation.

```julia
julia> @code_llvm f(SomeType(2))

; @ REPL[5]:1 within `f'
define i64 @julia_f_12189({ i64 } addrspace(11)* nocapture nonnull readonly dereferenceable(8)) {
top:
  ret i64 3
}
```

---

<div class="post-metadata">

### Author: ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)
#### Post date: [February 20, 2019, 8:20pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021/3 "2019-02-20T20:20:59Z")

</div>

Thanks. Yes, but that’s only one level.

For example, what if I’d like to dispatch on an abstract trait type from which SomeTrait is a subtype?

---

<div class="post-metadata">

### Author: ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)
#### Post date: [February 20, 2019, 8:41pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021/4 "2019-02-20T20:41:54Z")

</div>

so something more like the following?

```julia
abstract type AbstractSomeType end
struct SomeType <: AbstractSomeType
       x::Int 
end

abstract type AbstractSomeTrait end
struct SomeTrait <: AbstractSomeTrait end

hasSomeTrait(::AbstractSomeType) = SomeTrait()
hasSomeTrait(::Any) = nothing

f(x) = f(x,hasSomeTrait(x))
f(x,::AbstractSomeTrait) = 3
f(x,::Any) = 4

```

In this case we can see that it still uses static dispatch

```julia
julia> @code_llvm f(SomeType(2))

; @ REPL[7]:1 within `f'
define i64 @julia_f_12188({ i64 } addrspace(11)* nocapture nonnull readonly dereferenceable(8)) {
top:
  ret i64 3
}

```

---

<div class="post-metadata">

### Author: ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)
#### Post date: [February 20, 2019, 8:52pm UTC](https://discourse.julialang.org/t/trait-inheritance/21021/6 "2019-02-20T20:52:36Z")

</div>

That looks great. Thanks!
