Is there a parent type for all callable objects?
I am trying to constrain the arguments of a function to Function
+ constructors. Something like Union{<:Function,<:Constructor}
but Constructor
doesn't exist. Is there a general type that I can use in this scenario?
nope
I am considering Union{Function,Type}
. Experimenting with it...
why not Any
? there's nothing really generic you can do over "callables" anyway
Andy Dienes said:
why not
Any
? there's nothing really generic you can do over "callables" anyway
Ambiguity with other types. It is a more convoluted use case.
# supported argument types
const Callable = Union{Function,Type}
const Target = Union{Symbol,AbstractString}
const ColsCallableTarget = Pair{<:Any,<:Pair{<:Callable,<:Target}}
const ColsCallable = Pair{<:Any,<:Callable}
const CallableTarget = Pair{<:Callable,<:Target}
const MapArg = Union{ColsCallableTarget,ColsCallable,CallableTarget,Callable}
MapArg
is the type that I want to constrain at the end. My function definition will have a VargArg
of them:
function foo(arg::MapArg...)
end
The VarArg
requirement forces this non-idiomatic approach with explicit types. I would use simple dispatch if I could.
does does foo
do?
Let me finish the PR and you can see the whole picture :slight_smile:
julia> struct Foo end
julia> (::Foo)(x, y) = x + y
julia> Foo()(1, 2)
3
julia> supertype(Foo)
Any
Yes, I am thinking of types as possible callables.
that wasn't a type
julia> Foo() isa Type
false
Oh I see your point.
You are mentioning the case of an instance that is callable and not a subtype of Function
.
I don't want to support this use case.
It's callable :shrug:
I know. It wouldn't hurt to support this case, but since Julia doesn't have a Callable
super type, I don't need to cover it in practice.
The use case is pretty simple: take a function or constructor and map over an array of arguments. The PR is almost ready...
https://github.com/JuliaML/TableTransforms.jl/pull/305
The PR above should clarify the use case. It is working fine :slight_smile:
Júlio Hoffimann said:
Julia doesn't have a
Callable
super type
Technically there's https://github.com/JuliaLang/julia/blob/85687b522a6e73836b6ae95e65a8dc1723427d32/base/essentials.jl#L5 but it isn't public.
Interesting. The exact same definition.
For not-really-generic code it's totally fine to dispatch on Union{Function, Type}
to catch many callables, but in generic code there's no way to select callables just by their supertype. That's why generic API is designed so that there's no confusion – whether a callable should be in this argument or not.
Relevant discussion on the Julia issue tracker:
The design of Julia is such that anything may be callable, just give it a method. Thus a Callable
type would not make sense.
Yea, I've writen functions like (f::Foo)(x) = ...
before.
Last updated: Oct 18 2025 at 04:39 UTC