Need help with MethodError: ambigous


#1

How to solve this “ambigous” problem by fixing the syntax?

julia> struct Element
              a::Vector

              function Element(x::Vector)
                      new(x)
              end
       end

julia> function (elt_type::Type{T})(pts::Vector{Int}) where T<:Element
                 2
        end

julia> Element([1,3])
ERROR: MethodError: Element(::Array{Int64,1}) is ambiguous. Candidates:
  (elt_type::Type{T})(pts::Array{Int64,1}) where T<:Element in Main at REPL[2]:2
  Element(x::Array{T,1} where T) in Main at REPL[1]:5
Possible fix, define
  Element(::Array{Int64,1})


#2

Is this what you mean?

julia> struct Element
           a::Vector

           function Element(x::Vector)
               new(x)
           end
       end

julia> Element(pts::Vector{Int}) = 2
Element

julia> Element([1,3])
2

#3

I am sorry, the upper is my simplied version of the bellow code. How to fix it with your idea above?

julia> struct Element{DIM,N}
                    a::Int
                    b::Vector
                    function Element{DIM,N}(connectivity::Vector) where {DIM,N}
                         new(3, connectivity)
                   end
        end

julia> struct Point
                idx::Int
                coord::Vector{Float64}
                weight::Float64
       end

julia> function (elt_type::Type{T})(pts::Vector{Point}) where T<:Element
                 2
       end

julia> pts1 = Point(1, [23.0, 21.0, 32.0], 4.0);

julia> pts2 = Point(45, [23, 21, 2], 1.0);

julia> r = [pts1, pts2]
2-element Array{Point,1}:
 Point(1, [23.0, 21.0, 32.0], 4.0)
 Point(45, [23.0, 21.0, 2.0], 1.0)

julia> Element{2,4}(r)
ERROR: MethodError: Element{2,4}(::Array{Point,1}) is ambiguous. Candidates:
  (elt_type::Type{T})(pts::Array{Point,1}) where T<:Element in Main at REPL[3]:2
  (::Type{Element{DIM,N}})(connectivity::Array{T,1} where T) where {DIM, N} in Main at REPL[1]:5
Possible fix, define
  (::Type{Element{DIM,N}})(::Array{Point,1})


#4

Replace

function (elt_type::Type{T})(pts::Vector{Point}) where T<:Element
                 2
       end

with

function Element{DIM,N}(connectivity::Vector{Point}) where {DIM,N}
    2
end

maybe?

julia> Element{2,4}(r)
2