# Multiple dispatch in an inner method

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 23, 2019, 9:24am UTC](https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434 "2019-04-23T09:24:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 23, 2019, 9:24am UTC](https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434/1 "2019-04-23T09:24:17Z")

</div>

I seem to need to use multiple dispatch in an inner constructor. I’ve never needed it before, nor can I find any information about it in the docs, thought I’d ask here. Is there any particular problem or aspect I need to keep in mind when doing that?

The reason for this is the following MWE (stripped of everything but the essential parts):

```julia
abstract type AbstractPeriod end

abstract type Instantaneous <: AbstractPeriod end

abstract type Prolonged <: AbstractPeriod end

struct Period{T <: AbstractPeriod}
    anchor
    duration

    Period(s) = new{Instantaneous}(s, 0)
    Period(s, d) = new{Prolonged}(s, d)
end

struct Temporal{T <: AbstractPeriod}
    time::Period{T}
    # more fields...
end

struct POIType{T <: AbstractPeriod}
    name
    # more fields...
end

struct POI{T <: AbstractPeriod}
    type::POIType{T}
    temporal::Temporal{T}
    # more fields...
end

```

The whole gist of this setup is that `POI` has two fields whose types are containers whose type is identical. I’m using an abstract type to accomplish this, `AbstractPeriod`, and `Period` is using that abstract type depending on its arguments… If any of you has some insight on how to improve on this I’d love to hear it.

---

<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 23, 2019, 11:38am UTC](https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434/2 "2019-04-23T11:38:39Z")

</div>

I don’t think there is anything special going on here. That said, I wonder if you would be better off with two separate types for what seem to be two separate concepts.

Beware of the abstract field types though, but I am assuming that is just for the MWE.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 23, 2019, 11:42am UTC](https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434/3 "2019-04-23T11:42:36Z")

</div>

> [@Tamas\_Papp](#):
>
> I don’t think there is anything special going on here.

OK, that’s great to hear.

> [@Tamas\_Papp](#):
>
> That said, I wonder if you would be better off with two separate types for what seem to be two separate concepts.

I started off that way, but then I need to connect these two types in `POI` somehow. Still think about it.

> [@Tamas\_Papp](#):
>
> Beware of the abstract field types though, but I am assuming that is just for the MWE.

Yea, it’s just for the MWE.

Thanks!

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 23, 2019, 1:05pm UTC](https://discourse.julialang.org/t/multiple-dispatch-in-an-inner-method/23434/4 "2019-04-23T13:05:18Z")

</div>

Here’s what I coalesced on, so far:

```julia
abstract type AbstractPeriod end

abstract type Instantaneous <: AbstractPeriod end

abstract type Prolonged <: AbstractPeriod end

abstract type POIType{T <: AbstractPeriod} end

for i in (:Nest, :Feeder)
    @eval struct $i <: POIType{Instantaneous} end
end

for i in (:Track, )
    @eval struct $i <: POIType{Prolonged} end
end

abstract type DurationType{T <: AbstractPeriod} end

struct Interval <: DurationType{Prolonged}
    anchor::Nanosecond
    duration::Nanosecond
end

struct Instant <: DurationType{Instantaneous}
    anchor::Nanosecond
end

struct POI{P <: POIType, D <: DurationType}
    type::P
    temporal::D
    POI(type::P, temporal::D) where {T <: AbstractPeriod, P <: POIType{T}, D <: DurationType{T}} = new{P, D}(type, temporal)
end

```
