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

Partial parameter match leads to confusing MethodError #18110

Closed
timholy opened this issue Aug 18, 2016 · 6 comments
Closed

Partial parameter match leads to confusing MethodError #18110

timholy opened this issue Aug 18, 2016 · 6 comments
Labels
bug Indicates an unexpected problem or unintended behavior error handling Handling of exceptions by Julia or the user types and dispatch Types, subtyping and method dispatch

Comments

@timholy
Copy link
Member

timholy commented Aug 18, 2016

This is closely related to #17319, but distinct enough that I think it deserves its own issue.

Here's the problem:

julia> immutable Foo{N} end

julia> foo{T,N}(x::Union{AbstractArray{T,N},Foo{N}}) = 1
foo (generic function with 1 method)

julia> foo(rand(2,2))
1

julia> x = Foo{3}()
Foo{3}()

julia> foo(x)
ERROR: MethodError: no method matching foo(::Foo{3})
Closest candidates are:
  foo{T,N}(::Union{AbstractArray{T,N},Foo{N}}) at REPL[2]:1

Note that there's no red highlighting, which suggests that the arguments match (except they don't). Eventually you figure out that this happens because T is not a parameter of Foo. You can work around this by instead defining

typealias NDimType{N,T} Union{AbstractArray{T,N}, Foo{N}}
foo{N}(x::NDimType{N}) = 1

but a newbie will not discover this easily.

EDIT: in particular, unlike #17319 there is no warning given. At a minimum, the lack of a warning is a bug.

@timholy timholy added bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch labels Aug 18, 2016
@JeffBezanson
Copy link
Member

This situation is much less straightforward than #17319, since all parameters do occur in the signature and the method is indeed callable with some arguments. For this we'd need to compute whether there exists a type that can be determined to be a subtype of the signature without matching all of the static parameters. It seems like a pretty non-trivial problem to me.

@JeffBezanson
Copy link
Member

I think the easiest thing to do here (after #8974) will be to detect this situation in the MethodError printer and report that T was not matched (as opposed to giving a warning at definition time, though we could do that too if we figure out an algorithm for it).

@yuyichao
Copy link
Contributor

Is this more similar to #3738 ?

@JeffBezanson
Copy link
Member

It's related, but those were cases where the method should have matched (and now does), while this is a case where the method (correctly) won't match and better tooling is needed.

@timholy
Copy link
Member Author

timholy commented Aug 18, 2016

For this we'd need to compute whether there exists a type that can be determined to be a subtype of the signature without matching all of the static parameters. It seems like a pretty non-trivial problem to me.

It's probably mostly a lack of imagination on my part, but I'm having a hard time seeing how this would arise outside of the context of Unions. In such a case, isn't it sufficient to check every type in the Union and see whether it uses all of the parameters?

Or is the problem multi-argument functions, where some parameters may be fixed by the first argument, others by the second, and only certain combinations that fail to fix all of them?

@JeffBezanson JeffBezanson added the error handling Handling of exceptions by Julia or the user label Nov 4, 2016
@yuyichao
Copy link
Contributor

Fixed by #23117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior error handling Handling of exceptions by Julia or the user types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

3 participants