Why would you create a subtype of `Function`?

I am curious about the utility of creating abstract or concrete subtypes of Function.

Aside from syntactic convenience, I am not sure of the distinction between a singleton type that is given several methods and a function definition.

struct MulPlus1T end
mulplus1 = MulPlus1T()
(::MulPlus1T)(x) = x+1
(::MulPlus1T)(x,y) = x*y+1

I can add as many methods as I want to mulplus1, so it seems to do everything a Function can.

Then, what utility is there to making MyFuncType a subtype of <:Function? As far as I know, it would allow myfunc to be used in methods of higher order functions that specifically demand that the argument isa Function, but I usually do not see this in practice because it would exclude user defined callable objects (which perhaps might be a subtype of some other abstract type and thus cannot be a Function).

there are probably two million things you can do with julia typing system that is useless.

Function is used for dispatching mostly, not to create subtype of directly:

julia> sin isa Function
true

Fair point, but then why have Function in the first place?

Is there a case where you would actually want to dispatch on Function and explicitly disallow user defined callable objects?

I think this is related to the good’old traits vs types discussion…
If julia had traits you would dispatch on a Is_callable trait. Since julia is based on types you dispatch on the Function type…

1 Like