Is there a Function type which captures its return type

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

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

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

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

struct A{T <: Function}

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

Perhaps the excellent FunctionWrappers.jl would fit your needs?

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

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

using FunctionWrappers
import FunctionWrappers: FunctionWrapper

# A function that takes two Ts and returns a T.
struct ReturnCaptured{T}
  fn::FunctionWrapper{T, Tuple{T,T}}
(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)