Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary ambiguity warning? #5460

Closed
timholy opened this issue Jan 21, 2014 · 6 comments
Closed

Unnecessary ambiguity warning? #5460

timholy opened this issue Jan 21, 2014 · 6 comments

Comments

@timholy
Copy link
Member

timholy commented Jan 21, 2014

type A; end
type B; end

myfunc(a1::A, a2::A) = println("A,A")
myfunc(b1::B, b2::B) = println("B,B")
myfunc(a::A, b::B) = println("A,B")
myfunc(b::B, a::A) = println("B,A")
# In real life the next ones will call a function on obj and dispatch to one of the above
myfunc(obj, ab::Union{A,B}) = println("Any,AB")
myfunc(ab::Union{A,B}, obj) = println("AB,Any")
myfunc(obj1, obj2) = println("Any,Any")

This yields

julia> include("ambiguity.jl")
Warning: New definition 
    myfunc(Union(A,B),Any) at /tmp/ambiguity.jl:10
is ambiguous with: 
    myfunc(Any,Union(A,B)) at /tmp/ambiguity.jl:9.
To fix, define 
    myfunc(Union(A,B),Union(A,B))
before the new definition.

Is this truly ambiguous, since I've already defined myfunc for any combination of types A and B?

In my real-life implementation, defining myfunc(obj1::Union(A,B),obj2::Union(A,B)) will lead to a circular call, since it will dispatch to one of the specific instances.

@toivoh
Copy link
Contributor

toivoh commented Jan 21, 2014

It's not truly ambiguous, but I should think that it might be a bit tricky to detect this in the ambiguity detection machinery (Jeff will have to say whether this is within reach).
But I guess that you would be fine if you added

myfunc(obj1::Union(A,B),obj2::Union(A,B)) = error("This should never happen!")

since one of the four first myfunc methods should always be more specific than the method above in practice?

@timholy
Copy link
Member Author

timholy commented Jan 21, 2014

Good suggestion. @JeffBezanson, if this is hard to fix, I propose we just add this to the FAQ (or someplace).

@elextr
Copy link

elextr commented Jan 22, 2014

As I read it, f(Any, union(A,B)) and f(union(A,B),Any) is exactly the situation mentioned in http://docs.julialang.org/en/release-0.2/manual/methods/#method-ambiguities and, since Any can match more than A and B the other methods that have been defined are insufficient to remove that ambiguity.

@toivoh
Copy link
Contributor

toivoh commented Jan 22, 2014

The ambiguous case would be f(::union(A,B), ::union(A,B)), but what I and Tim argue is that it is actually covered by the first four method definitions for f (f(::A,::A) etc). The issue is about whether the compiler could be made to infer this.

@elextr
Copy link

elextr commented Jan 22, 2014

It was just a reply to @timholy suggesting that its already covered in the manual (though of course it can't hurt to repeat common problems in the FAQs sometimes).

Inferring the method taking unions would hide the ambiguity, which in your case is your intention, but it may also infer methods that hide accidental ambiguities, which I would argue is a bad thing, especially in more subtle situations than the plain two argument Any situation here.

Adding the one liner you suggested tells Julia your intentions.

[pao: fix spelling of @timholy's username]

@timholy
Copy link
Member Author

timholy commented May 8, 2016

Closed by #16125

@timholy timholy closed this as completed May 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants