# API for specifying which version of a function to use in a package

**URL:** https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047
**Category:** General Usage
**Created:** [January 31, 2020, 10:19pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047 "2020-01-31T22:19:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 31, 2020, 10:19pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/1 "2020-01-31T22:19:07Z")

</div>

In [`IntervalArithmetic.jl`](https://github.com/JuliaIntervals/IntervalArithmetic.jl) we need a way to choose between different versions of a function (e.g. the power function) that have different performance / accuracy tradeoffs.

Is there a standard way to have an API for that? Maybe something along the lines of

```julia
abstract type APIChoice end
abstract type PowerAPIChoice <: APIChoice end

struct AccuratePowers <: PowerAPIChoice end
struct FastPowers <: PowerAPIChoice end

IntervalArithmetic.specify!(AccuratePowers())
IntervalArithmetic.specify!(FastPowers())

```

The implementation would be, for the moment, to redefine all the corresponding functions appropriately. Strictly speaking this should be some kind of contextual dispatch, however.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 31, 2020, 10:36pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/2 "2020-01-31T22:36:45Z")

</div>

“contextual dispatch” sounds like “trait-based dispatch” where the trait selects over chosen-behavior appropriate algorithms rather than utilized-data-type appropriate algorithms.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 31, 2020, 11:01pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/3 "2020-01-31T23:01:01Z")

</div>

Yes pretty much. It’s a phrase that I learnt from the Cassette.jl world.

Basically you want to do a whole calculation inside a scope where you choose “accurate powers” or “fast powers”. For e.g. testing you may want to be able to run in either of those two situations and record the respective results and compare them.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [February 1, 2020, 1:14pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/4 "2020-02-01T13:14:55Z")

</div>

Maybe the [`@dynamo`](https://mikeinnes.github.io/IRTools.jl/latest/dynamo/) macro from IRTools.jl could be useful?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 1, 2020, 1:33pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/5 "2020-02-01T13:33:30Z")

</div>

That seems very much overkill?

In C++ you can give something called “execution policy” to algorithms ([std::execution::sequenced\_policy, std::execution::parallel\_policy, std::execution::parallel\_unsequenced\_policy, std::execution::unsequenced\_policy - cppreference.com](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t)). These are unique types just passed in as an argument to the algorithm. This is pretty much exactly the same as what you proposed in the first post and seems like a good idea to me.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [February 1, 2020, 1:39pm UTC](https://discourse.julialang.org/t/api-for-specifying-which-version-of-a-function-to-use-in-a-package/34047/6 "2020-02-01T13:39:04Z")

</div>

I use to dispatch on procedures which make sense for a given data type and chosen method quite often

```julia
function solve(method, data)
     data = step1(method, data)
     data = step2(method, data)
     return data
end

solve(MyMethod(arg=true), mydata)

```

Isn’t that just multiple dispatch in practice?
