# How to assign a particular method for a function with the exact type signature to an lvalue?

**URL:** https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707
**Category:** General Usage
**Tags:** question
**Created:** [November 3, 2022, 10:26am UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707 "2022-11-03T10:26:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)
#### Post date: [November 3, 2022, 10:26am UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/1 "2022-11-03T10:26:23Z")

</div>

I’d like to be able to set an lvalue to a particular method. Essentially use it as a function pointer so that it can be called later in some other code. I’d like to set the method rather than the function because I’m worried about type stability for the return type. A simple example of assigning the function is provided below, but I would like to assign a particular method.

```julia
abc(x::Int) = return 2.0
abc(x::Float32) = return 2

def = abc

```

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [November 3, 2022, 10:40am UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/2 "2022-11-03T10:40:10Z")

</div>

Not sure if it is a good way to ensure type stability… but you could use `invoke`, e.g.  
`def(x::Int) = invoke(ABC, Tuple{Int}, x)`.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 3, 2022, 12:24pm UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/3 "2022-11-03T12:24:33Z")

</div>

you probably don’t need to do this.

---

<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: [November 3, 2022, 1:40pm UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/4 "2022-11-03T13:40:10Z")

</div>

Take a look at FunctionWrappers.jl

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [November 3, 2022, 1:49pm UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/5 "2022-11-03T13:49:05Z")

</div>

Base julia does not have syntax for this, because dispatching to a method given a function name and arguments is a central pillar of its design. What problem are you trying solve?

---

<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: [November 3, 2022, 4:01pm UTC](https://discourse.julialang.org/t/how-to-assign-a-particular-method-for-a-function-with-the-exact-type-signature-to-an-lvalue/89707/6 "2022-11-03T16:01:01Z")

</div>

The question/problem seems perfectly clear:

> [@prittjam](#):
>
> essentially use it as a function pointer so that it can be called later in some other code. I’d like to set the method rather than the function because I’m worried about type stability for the return type.

[GitHub - yuyichao/FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl) was made for this use case.

As an example:

```julia
using FunctionWrappers: FunctionWrapper

abc(x::Int) = 1.0
abc(x::Float32) = 2

abc_wrap = convert(FunctionWrapper{Int, Tuple{Float32}}, abc)
abc_wrap(5.0f0) # returns 2 and is type stable

```

Basically, this can precompute the lookup that the dynamic dispatch would typically do at runtime. This can be useful if you want to e.g. have an array of function (pointers) and select them by index where all functions have the same signature + return value.

Note, that this does make you vulnerable to #265:

```julia
julia> abc(x::Float32) = 5; # redefine function

julia> abc(5.0f0)
5

julia> abc_wrap(5.0f0)
2

```
