# Abstract function with imposed arguments and return types

**URL:** https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007
**Category:** General Usage
**Tags:** question
**Created:** [May 31, 2017, 6:35am UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007 "2017-05-31T06:35:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)
#### Post date: [May 31, 2017, 6:35am UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/1 "2017-05-31T06:35:55Z")

</div>

I would like to define a struct where one of the field is a function. But I would also like to control the arguments and return type of that fonction. For the sake of the example, let say that function would match this signature : `::Int -> ::Float64`

Currently I use the following struct that is too permissive to my taste

```julia
struct MyType
  my_function::Function
end

Is there any way to inforce the signature of `my_function` in Julia (v0.6)?
```

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [May 31, 2017, 6:39am UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/2 "2017-05-31T06:39:11Z")

</div>

for correctness, use `::Any` (or leave it off entirely)

additional discussion at [Function Parameter Speculation #17168](https://discourse.julialang.org/t/function-parameter-speculation-17168/1677)

---

<div class="post-metadata">

### Author: ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)
#### Post date: [May 31, 2017, 6:39am UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/3 "2017-05-31T06:39:53Z")

</div>

This might be what you are looking for:

[https://github.com/yuyichao/FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl)

---

<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: [May 31, 2017, 12:58pm UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/4 "2017-05-31T12:58:46Z")

</div>

Use

```julia
struct MyType{F} # or {F<:Function}
  my_function::F
end

```

if performance is important.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [May 31, 2017, 1:51pm UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/5 "2017-05-31T13:51:44Z")

</div>

It would be really nice if `FunctionWrappers` were in `Base`. Very few people would have the expertise to write that package, so it would be nice if there were some sort of maintenance guarantee built in to it. As far as I know, there is no other way to do that kind of super-efficient unboxing of functions in Julia.

---

<div class="post-metadata">

### Author: ![Azzaare](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azzaare/32/21008_2.png) [@Azzaare](https://discourse.julialang.org/u/Azzaare)
#### Post date: [June 1, 2017, 3:23am UTC](https://discourse.julialang.org/t/abstract-function-with-imposed-arguments-and-return-types/4007/6 "2017-06-01T03:23:10Z")

</div>

Thank you all. I have some reading to do now.

I will finish the tests and coverage of my package and try to improve the efficiency again at that point.
