# Call Generic Function From Specialization

**URL:** <https://discourse.julialang.org/t/call-generic-function-from-specialization/730>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 4, 2016, 10:37pm UTC](https://discourse.julialang.org/t/call-generic-function-from-specialization/730 "2016-12-04T22:37:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![friedmud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/friedmud/32/277_2.png) [@friedmud](https://discourse.julialang.org/u/friedmud)\
**Post date:** [December 4, 2016, 10:37pm UTC](https://discourse.julialang.org/t/call-generic-function-from-specialization/730/1 "2016-12-04T22:37:13Z")

</div>

Having a tough time calling a more “generic” form a function from one that is specialized (i.e call a function for a “base” type from a version of the function for a subtype).

Consider this:

```julia
abstract foo

type bar <: foo
end

function doStuff(a::foo)
  println("Generic!")
end

function doStuff(a::bar)
  println("Specialized!")
  doStuff(convert(foo, a))
end

b = bar()
doStuff(b)

```

That `doStuff(convert(foo, a))` represents my obviously failing attempt to do this… as this code goes into infinite recursion calling `doStuff(a::bar)`.

I can see a way to fix it by making `doStuff()` parametric and then explicitly specifying the parametric type to call… but I was hoping to avoid that (or, I guess, I was thinking that there should be a way to do this without resorting to that). Surely I’ve just missed something important here…

Any advice would be appreciated!

Derek

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [December 4, 2016, 10:54pm UTC](https://discourse.julialang.org/t/call-generic-function-from-specialization/730/2 "2016-12-04T22:54:08Z")

</div>

Instead of

```julia
doStuff(convert(foo, a))

```

you want

```julia
invoke(doStuff,(foo,),a)

```

See under [Essentials](http://docs.julialang.org/en/release-0.5/stdlib/base/) in the manual.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [December 5, 2016, 9:42am UTC](https://discourse.julialang.org/t/call-generic-function-from-specialization/730/3 "2016-12-05T09:42:53Z")

</div>

i would probably make an internal function

```julia
function _dostuff(a::Foo)
  println("Generic!")
end

dostuff(a::foo) = _dostuff(a)

function dostuff(a::Bar)
  println("Specialized!")
  _dostuff(a)
end

```

however, i feel that based on the actual case, there should be a nicer solution. i assume there is some kind of branching going on in dostuff(::Bar), which might be done compile time through dispatch.

---

<div class="post-metadata">

**Author:** ![friedmud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/friedmud/32/277_2.png) [@friedmud](https://discourse.julialang.org/u/friedmud)\
**Post date:** [December 5, 2016, 7:14pm UTC](https://discourse.julialang.org/t/call-generic-function-from-specialization/730/4 "2016-12-05T19:14:26Z")

</div>

That works perfectly Ralph!

The documentation is pretty hilarious… it matches my use case perfectly. I have no idea how I wasn’t able to find `invoke()` on my own… I appreciate the heads up!

For completeness… here is the documentation for invoke():

> `invoke(f, (types...), args...)`  
> Invoke a method for the given generic function matching the specified types (as a tuple), on the specified arguments. The arguments must be compatible with the specified types. This allows invoking a method other than the most specific matching method, which is useful when the behavior of a more general definition is explicitly needed (often as part of the implementation of a more specific method of the same function).
