Consider calls such as these:
julia> evalpoly(3, ())
ERROR: BoundsError: attempt to access Tuple{} at index [0]
Stacktrace:
[1] getindex(t::Tuple, i::Int64)
@ Base ./tuple.jl:31
[2] macro expansion
@ ./math.jl:182 [inlined]
[3] evalpoly(x::Int64, p::Tuple{})
@ Base.Math ./math.jl:181
[4] top-level scope
@ REPL[1]:1
julia> evalpoly(0.3, Float64[])
ERROR: BoundsError: attempt to access 0-element Vector{Float64} at index [0]
Stacktrace:
[1] getindex
@ ./essentials.jl:13 [inlined]
[2] _evalpoly
@ ./math.jl:199 [inlined]
[3] evalpoly(x::Float64, p::Vector{Float64})
@ Base.Math ./math.jl:194
[4] top-level scope
@ REPL[2]:1
Some issues:
-
The errors are not friendly/decriptive.
-
In the case when the coefficients are an empty tuple,
Tuple{}
, it might make sense to fail early, that is, make this aMethodError
by preventing dispatch to the method for empty tuples in the first place. This trick should work. -
Arguably, a polynomial with no coefficients should be interpreted as the zero polynomial. So it might make sense to special-case this instead of erroring. For tuples this might be addressed with a method like so:
Base.evalpoly(::T, ::Tuple{}) where {T} = zero(T)
. For empty vectors an additional check would need to be included at the beginning of the relevant method.