MethodError: no matching method for Int64 despite specifying Real

I’ve looked at this question on the Julia discourse and checked parametric types in the docs (which are fantastic docs, I’d like to add) and I’m not sure what to do next.

I have a function which wants tuples of numbers, making sure to specify <:Real instead of just Real:

function point_within(point::Tuple{<:Real, <:Real}, polygon::Vector{Tuple{<:Real, <:Real}})

Yet, giving it Int64s yields the following error:

LoadError: MethodError: no method matching point_within(::Tuple{Int64, Int64}, ::Vector{Tuple{Int64, Int64}})
Closest candidates are:
point_within(::Tuple{Real, Real}, ::Vector{Tuple{Real, Real}})

I’d be most grateful to know best practise for dealing with abstract parametric types and if possible, something to replace the above example of an offending function definition with.

Try

function point_within(point::Tuple{<:Real, <:Real}, polygon::Vector{<:Tuple{<:Real, <:Real}})
1 Like

Ah, it hadn’t occurred to me to make sure <: is applied through all levels.

That has worked great, thanks.

I think Vector{<:Tuple{Real, Real}} should work as well as Tuples are covariant (if I remember the term correctly)

4 Likes

This is correct.