# Is there a Function type which captures its return type

**URL:** <https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890>\
**Category:** New to Julia\
**Tags:** question, type, function\
**Created:** [March 10, 2021, 3:10pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890 "2021-03-10T15:10:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [March 10, 2021, 3:10pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890/1 "2021-03-10T15:10:29Z")

</div>

I have a struct which should contain a parametrized function returning a certain type.  
Can I impose this type restriction onto the struct field?

```julia
using Unitful
struct a 
T::Function # This will allow any function
end

obj = a((t)->3u"K/s"*t)
energy = obj.T(1u"s")

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 10, 2021, 3:15pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890/2 "2021-03-10T15:15:00Z")

</div>

It doesn’t go the whole way but for efficient code you probably want

```julia
struct A{T <: Function}
    f::T
end

```

by parameterizing your struct the compiler can actually keep track of the specific intricies of the function `f` (including inferrring its return type automatically)

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [March 10, 2021, 3:25pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890/3 "2021-03-10T15:25:10Z")

</div>

Perhaps the excellent [FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl) would fit your needs?

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [March 10, 2021, 3:38pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890/4 "2021-03-10T15:38:03Z")

</div>

This looks interesting. Sadly, I could not find any documentation on how to use it correctly.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [March 10, 2021, 4:08pm UTC](https://discourse.julialang.org/t/is-there-a-function-type-which-captures-its-return-type/56890/5 "2021-03-10T16:08:38Z")

</div>

Taking a look at the test files might help. Here’s a little something to get you started:

```julia
using FunctionWrappers
import FunctionWrappers: FunctionWrapper

# A function that takes two Ts and returns a T.
struct ReturnCaptured{T}
  fn::FunctionWrapper{T, Tuple{T,T}}
end
(f::ReturnCaptured{T})(x::T, y::T) where{T} = f.fn(x,y)

my_cool_function = ReturnCaptured{Float64}((x,y)->sqrt(abs2(x-y)))

my_cool_function(1.0, 2.0)

```
