Specifying output container type based on input container

Here’s a hacky attempt to inform the compiler about the container type (using How to get the container type of a container?):

constructor_of(::Type{T}) where T =
    getfield(T.name.module, Symbol(T.name.name))

function f(x::A) where {T<:Real, N, A<:AbstractArray{T, N}}
   dropdims(sum(x, dims=(1,)), dims=(1,))::constructor_of(A){T,N-1}
end

where you would wrap the entire function block with ::constructor_of(A){T,N-1}.

But surely there’s a way to do it directly in the function signature?


One other attempt:

function f(x::A)::(A.name.wrapper){T,N-1} where {T<:Real, N, A<:AbstractArray{T, N}}
   dropdims(sum(x, dims=(1,)), dims=(1,))
end

this feels illegal though. Not sure if A.name.wrapper is stable or not?