Syntactic sugar for many arguments with same type annotation but separately dispatchable?

Is there a syntactic sugar for a function where the arguments must all have a type T<:SomeType, but each one could be a different type?

What I want to do might be clearer by example. So at the moment I might write a function like the following:


function afunk(arg1::T1, arg2::T2, arg3::T3)  where T1<:Real where T2<:Real where T3<:Real
    ...
end

But I was wondering if there is a way to group those, something like:

function afunk(arg1::T1, arg2::T2, arg3::T3)  where T1,T2,T3<:Real
    ...
end

(The latter syntax doesn’t work, just trying to demonstrate approximately what I’m after in case it is not clear.)

It might not seem that useful from the above example, but for my preference, it might stop me using a type alias like suggested in the following discussion as it would keep my function signatures neat and readable:

I don’t think there is a shorthand for this purpose. You have to spell out all the types and can’t combine <: relations. If you are not using T1 etc, you can of course just use

function afunk(arg1::Real, ...
1 Like

Adding on to this, you can follow @Tamas_Papp’s suggestion and then, if you still need to refer to the types of the arguments inside the function, you can do it with typeof() which should still not introduce any overhead:

julia> function f(a1::Real, a2::Real)
         T1 = typeof(a1)
         println(T1)
       end
f (generic function with 1 method)

julia> f(1.0, 2)
Float64
3 Likes