I am trying to get the following toy snippet type-stable, but wasn’t sure how to proceed.
using Random
using InteractiveUtils
using LinearAlgebra
const T = Float64
# scalar ver
function foo(
a ::D,
b ::AbstractVector{T};
c ::AbstractVector{T}=similar(b),
) where {D<:Real, T<:Real}
c[begin] = b[begin] + a
return last(c)
end
# vector ver
function vfoo(
va ::AbstractArray{D},
vb ::AbstractArray{<:AbstractVector{T}};
vc ::AbstractArray{<:AbstractVector{T}}=similar(b),
) where {D<:Real, T<:Real}
((a, b, c)->foo(a, b; c=c)).(va, vb, vc)
return last.(vc)
end
# scalars
a = T(randn())
b = [T(randn())]
c = [T(randn())]
@code_warntype foo(a, b; c=c)
# vectors
va = [copy(a), copy(a)]
vb = [copy(b), copy(b)]
vc = [copy(c), copy(c)]
@code_warntype vfoo(va, vb; vc=vc)
Why would you want to limit your API ? This seems like a shame : if someone want to use your code for something you did not know it could be used for, then they can’t.
Would you also have time to share about how this one was different from the original one?
The type annotation of julia has been quite confusing for me, espacially those involving Abstract types…