How do I type lambda functions?

I want to do something like this:

struct Foo
  myFunc::Function{Int,Float64} # invalid Julia
end
zee(a::Int, b::Float64) = a+b
bar = Foo(zee)

This works:

struct Foo
  myFunc::Function
end
zee(a::Int, b::Float64) = a+b
bar = Foo(zee)

but I’m not sure that’s the best way to achieve your goal in Julia. What do you want to use this code for?

For organization purposes. It helps to clarify the interface design.

Julia’s type system doesn’t have what is common known as “arrow types” in other languages (eg Haskell), so you can’t do this.

Also, note that it may not even make sense conceptually, since adding a new method would just extend the “type” of a function, even if it would not necessarily make Julia recompile code for existing call paths.

That said, functions have a type, which is really a singleton. You can obtain it with typeof. For the example above, use something like

struct Foo{T}
    f:T
end

Note that I didn’t subtype to <:Function, callables can behave as functions.

1 Like

Does this parametrization have a performance benefit?

https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container-1