I have a question about generic functions which is probably something simple I am missing. Let’s say I have a function f
defined as follows:
function f(x::A)::AbstractArray{T, N-1} where {T<:Real, N, A<:AbstractArray{T, N}}
dropdims(sum(x, dims=(1,)), dims=(1,))
end
This function takes an arbitrary array x
as input, and specifies to the compiler that an array will be returned ::AbstractArray{T,N-1}
, i.e., with the same parameters T
, and 1 fewer dimension.
However, is there a way I can specify that an array of the same container type as x
will always be returned? For example, if x
is an Array{T,2}
, I want to indicate that an Array{T,1}
will also be returned - not just any AbstractArray
. Similar for StaticArrays
, etc.
I tried the obviously incorrect expression
function f(x::A)::A{T,N-1} where {T<:Real, N, A<:AbstractArray{T, N}}
dropdims(sum(x, dims=(1,)), dims=(1,))
end
which does not work. Is there a way to do this?
I should also specify - for this simple function I am sure the compiler can figure it out. But for more complex functions in my codebase I find it helped to indicate the output type… so perhaps indicating the correct container type will help as well.
Thanks!
Miles