Question about type ambiguity

61 views
Skip to first unread message

esproff

unread,
Oct 12, 2016, 1:22:04 PM10/12/16
to julia-users
Consider the code:

abstract AbstractFoo

type Foo <: AbstractFoo
end

f(x::AbstractFoo, y::Integer) = "method 1"
f(x::Foo, y::Real) = "method 2"

foo = Foo()
f(foo, 1)

This code results in an ambiguity error, since both methods contain one argument with a more specific type declaration than the other.  Now consider this code:

abstract AbstractFoo

type Foo <: AbstractFoo
end

f(x::AbstractFoo, y::Integer) = "method 1"
f{T<:Real}(x::Foo, y::T) = "method 2"

foo = Foo()
f(foo, 1)

This code, in my opinion, should work, since the second method spawns a bunch of sub-methods, one for each concrete subtype of Real in the second argument, and thus one of these sub-methods should be

f(x::Foo, y::Int) = "method 2"

which then eliminates the ambiguity, but it doesn't behave this way, why is that?

Yichao Yu

unread,
Oct 12, 2016, 2:03:32 PM10/12/16
to Julia Users
On Wed, Oct 12, 2016 at 1:22 PM, esproff <esp...@gmail.com> wrote:
Consider the code:

abstract AbstractFoo

type Foo <: AbstractFoo
end

f(x::AbstractFoo, y::Integer) = "method 1"
f(x::Foo, y::Real) = "method 2"

foo = Foo()
f(foo, 1)

This code results in an ambiguity error, since both methods contain one argument with a more specific type declaration than the other.  Now consider this code:

abstract AbstractFoo

type Foo <: AbstractFoo
end

f(x::AbstractFoo, y::Integer) = "method 1"
f{T<:Real}(x::Foo, y::T) = "method 2"

foo = Foo()
f(foo, 1)

This code, in my opinion, should work, since the second method spawns a bunch of sub-methods, one for each concrete subtype of Real in the second argument, and thus one of these sub-methods should be

No the set of signature the second method matches in the second case is the same as that in the first case.
 

Mauro

unread,
Oct 12, 2016, 2:41:52 PM10/12/16
to julia...@googlegroups.com
However, somewhat similar, this does not error:

julia> g(x) = 1
g (generic function with 1 method)

julia> g{X}(x::X) = 2
g (generic function with 2 methods)

julia> g(4)
2

So here the parameterized method does seem more specific.

Yichao Yu

unread,
Oct 12, 2016, 2:50:02 PM10/12/16
to Julia Users
This is missing method overwite. i.e. a bug.
 

Mauro

unread,
Oct 12, 2016, 3:23:51 PM10/12/16
to julia...@googlegroups.com
You mean the method-overwritten warning? But it generates two methods,
so maybe it should go to the ambiguity error?

Also similar for two arguments:

julia> h(x,y) = 1
h (generic function with 1 method)

julia> h{X}(x::X,y) = 2
h (generic function with 2 methods)

julia> h{X,Y}(x::X,y::Y) = 3
h (generic function with 3 methods)

julia> h{Y}(x,y::Y) = 4
h (generic function with 4 methods)

julia> h(4,5)
2

I'll open an issue.

Mauro

unread,
Oct 12, 2016, 5:10:39 PM10/12/16
to julia...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages