# How to reference the type of a function in a struct?

**URL:** https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919
**Category:** New to Julia
**Tags:** question
**Created:** [October 29, 2018, 12:59pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919 "2018-10-29T12:59:17Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)
#### Post date: [October 29, 2018, 12:59pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/1 "2018-10-29T12:59:17Z")

</div>

This is the type I want to use:

```
struct S 
    F::Function
    n::Int
end

```

However I want to allow only functions F which are of exactly this type:

```
 F(n::Int, k::Int)::BigInt

```

How can I express this in the struct S?

---

<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: [October 29, 2018, 1:10pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/2 "2018-10-29T13:10:05Z")

</div>

You cannot. Julia does not encode the arguments and return-type of functions (or more like “methods”) in their type.

Side note, if you care about performance, then you should parameterize on the function-type:

```julia
struct S{T<:Function}
    F::T
    n::Int
end

```

---

<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: [October 29, 2018, 1:26pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/3 "2018-10-29T13:26:39Z")

</div>

There is FunctionWrappers.jl.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 29, 2018, 1:32pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/4 "2018-10-29T13:32:17Z")

</div>

You cannot express it in the struct directly, but you can enforce something like this in the constructor, by checking the output of `Base.return_types(F, (Int, Int))`.

---

<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: [October 29, 2018, 1:58pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/5 "2018-10-29T13:58:07Z")

</div>

> [@DNF](#):
>
> You cannot express it in the struct directly, but you can enforce something like this in the constructor, by checking the output of `Base.return_types(F, (Int, Int))` .

This is not really a good idea. You are tying the logic of your code to inference (which is a heuristic) and the result will be very brittle.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 29, 2018, 2:00pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/6 "2018-10-29T14:00:55Z")

</div>

Is it the implementation that makes it brittle, or is the goal of restricting the input/output types of `F` inherently problematic?

---

<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: [October 29, 2018, 2:15pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/7 "2018-10-29T14:15:18Z")

</div>

Something like (updated thanks @Tamas_Papp)

```julia
struct S 
    F::Function
    n::Int
    S(F::Function, n::Int) = new( (n::Int, k::Int) -> F(n, k)::BigInt, n)
end

```

might be ok?

But any attempt to try to error when the struct `S` is created (as opposed to when `S.F` is used) seems difficult.

---

<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: [October 29, 2018, 2:36pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/8 "2018-10-29T14:36:32Z")

</div>

> [@kristoffer.carlsson](#):
>
> might be ok?

Since the closure `isa Function`, I guess you need some extra step not to get an infinite loop.

---

<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: [October 29, 2018, 2:38pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/9 "2018-10-29T14:38:53Z")

</div>

Thanks, updated.

---

<div class="post-metadata">

### Author: ![Bruno\_Amorim](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bruno\_Amorim](https://discourse.julialang.org/u/Bruno_Amorim)
#### Post date: [October 29, 2018, 5:55pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/10 "2018-10-29T17:55:10Z")

</div>

Would in this case still be advantageous to parametrize the sctruc with the function-type as proposed by @mauro3 ?

---

<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: [October 29, 2018, 6:02pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/11 "2018-10-29T18:02:53Z")

</div>

Likely, yes.

---

<div class="post-metadata">

### Author: ![Bruno\_Amorim](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bruno\_Amorim](https://discourse.julialang.org/u/Bruno_Amorim)
#### Post date: [October 29, 2018, 6:24pm UTC](https://discourse.julialang.org/t/how-to-reference-the-type-of-a-function-in-a-struct/16919/12 "2018-10-29T18:24:08Z")

</div>

Thank you!
