What is ambiguous here?

Hi,

Why is this reported as ambiguous, and is it true? Practice shows it is not.

using Test
x = detect_ambiguities(GMT)

x[2]
(getindex(D::GMTdataset{T, N}, inds::Vararg{Int64, N}) where {T, N} @ GMT C:\Users\j\.julia\dev\GMT\src\gmt_types.jl:122,
 getindex(D::GMTdataset{T, N}, inds::Vararg{Symbol, N}) where {T, N} @ GMT C:\Users\j\.julia\dev\GMT\src\gmt_types.jl:137)
1 Like

ambiguous when N = 0

3 Likes

But in that case GMTdataset{T, N} would be empty and an error would have occurred before when trying to access an empty object.

Sure, but Julia doesn’t know that by the method signatures alone. Simple example:

julia> f(::Array{T,N}, ::Vararg{Int64, N}) where {T,N} = "ints!"
f (generic function with 1 method)

julia> f(::Array{T,N}, ::Vararg{Symbol, N}) where {T,N} = "symbols!"
f (generic function with 2 methods)

julia> f(zeros((3, 5)), 1, 2)
"ints!"

julia> f(zeros((3,)), :one)
"symbols!"

julia> f(zeros(()))
ERROR: MethodError: f(::Array{Float64, 0}) is ambiguous. Candidates:
  f(::Array{T, N}, ::Vararg{Int64, N}) where {T, N} in Main at REPL[19]:1
  f(::Array{T, N}, ::Vararg{Symbol, N}) where {T, N} in Main at REPL[20]:1
Possible fix, define
  f(::Array{T, 0}) where T
4 Likes

Ok, thanks, I understand that case. But shouldn’t it be equivalent to this case where the GMTdataset type is used? No error here.

julia> D = GMTdataset()
String[]

julia> typeof(GMTdataset())
GMTdataset{Float64, 2}

julia> length(GMTdataset())
0

only if I do

julia> D[1]
ERROR: BoundsError: attempt to access 0Ă—0 GMTdataset{Float64, 2} at index [1]

So, the ambiguity never really happens.

The ambiguity never happens but it still exists.

That’s a 2-dimensional dataset with zero elements. A theoretical 0-dimensional dataset would be a GMTdataset{Float64, 0} and have exactly one element.

I think it’s worth adding that ambiguities aren’t necessarily a problem. Yes, they’re sometimes code smells of a bad design and they sometimes describe a case that you forgot about, but they can also describe an operation that’s otherwise nonsensical.

It can be useful to keep Test.detect_ambiguities empty just to help you find those problem cases, but there’s otherwise nothing wrong with leaving some ambiguities laying around.

2 Likes