Skip to content

Convert dispatch depends on the method order? #12939

@maximsch2

Description

@maximsch2

Using a sample code:

abstract Abs
type Foo <: Abs
end


type Bar
  val::Int64
end

type Baz
  val::Int64
end


import Base.convert


bar{T <: Abs}(x::T) = 2
bar(x) = 1

baz(x) = 1
baz{T <: Abs}(x::T) = 2


convert{T <: Abs}(::Type{Bar}, x::T) = Bar(2)
convert(::Type{Bar}, x) = Bar(1)


convert(::Type{Baz}, x) = Baz(1)
convert{T <: Abs}(::Type{Baz}, x::T) = Baz(2)


@show convert(Bar, Foo())
@show convert(Baz, Foo())

@show bar(Foo())
@show baz(Foo())

I get this on julia 0.3.11:

convert(Bar,Foo()) => Bar(2)
convert(Baz,Foo()) => Baz(2)
bar(Foo()) => 2
baz(Foo()) => 2

and this on latest 0.4:

convert(Bar,Foo()) = Bar(1)
convert(Baz,Foo()) = Baz(2)
bar(Foo()) = 2
baz(Foo()) = 2

Is this supposed to happen? Note that the only difference between Bar and Baz is in the method definition order and it only happens with convert

Metadata

Metadata

Assignees

No one assigned

    Labels

    types and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions