# Multiple Dispatch - Call Abstract Type Function

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-call-abstract-type-function/47311>\
**Category:** General Usage\
**Created:** [September 26, 2020, 1:00pm UTC](https://discourse.julialang.org/t/multiple-dispatch-call-abstract-type-function/47311 "2020-09-26T13:00:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhil\_teja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhil_teja/32/15235_2.png) [@akhil\_teja](https://discourse.julialang.org/u/akhil_teja)\
**Post date:** [September 26, 2020, 1:00pm UTC](https://discourse.julialang.org/t/multiple-dispatch-call-abstract-type-function/47311/1 "2020-09-26T13:00:28Z")

</div>

I’m new to Julia and I have a problem with multiple dispatch. Consider below code:

```julia
abstract type A end

struct A1<:A end
struct A2<:A end

foo(a::A) = println("It's A")

function foo(a::A1)
    # Do Something with A1 type object and then call foo(a::A)
    foo(a)
end

```

I create two objects a1 and a2

```julia
a1 = A1()
a2 = A2()

```

Now when I call foo(a1) I get StackOverFlow Error. I understand that it’s getting recursive, but I want to do some other operations before I call foo(a::A) type function for A1 type object.

```julia
ERROR: StackOverflowError:
Stacktrace:
 [1] foo(::A1) at ./REPL[5]:3 (repeats 79984 times)

```

Calling foo(a2) doesn’t give any error.

```julia
It's A

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 26, 2020, 1:09pm UTC](https://discourse.julialang.org/t/multiple-dispatch-call-abstract-type-function/47311/2 "2020-09-26T13:09:07Z")

</div>

Inside of `foo(a1)` you are calling `foo(a1 :: A1)` again. Hence you have infinite recursion and stack overflow.

To avoid this, consider factoring out the generic part into a separate function:

```julia
foo_common(a :: A) = println("It is A");

foo(a :: A) = foo_common(a);

function foo(a :: A1)
  # other things
  foo_common(a);
end

```

---

<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:** [September 26, 2020, 3:12pm UTC](https://discourse.julialang.org/t/multiple-dispatch-call-abstract-type-function/47311/3 "2020-09-26T15:12:46Z")

</div>

There is also `invoke`, however, @hendri54’s solution is more Julian, imo.
