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

Multiple dispatch issue with optional parameters #21481

Closed
spurll opened this issue Apr 21, 2017 · 4 comments
Closed

Multiple dispatch issue with optional parameters #21481

spurll opened this issue Apr 21, 2017 · 4 comments

Comments

@spurll
Copy link
Contributor

spurll commented Apr 21, 2017

In investigating what appeared to be a code coverage issue with CSV.jl, @omus and I stumbled upon an unusual dispatch issue. It can be summarized like this:

julia> function foo(x=Type)
           println("foo")
       end;

julia> function foo{T}(x::T)
           println("Method foo{T}")
       end;

julia> foo()
Method foo{T}

As @omus has pointed out, the issue is that this:

function foo(x=Type)
    println("foo")
end

is lowered to:

function foo()
    foo(Type)
end

function foo(x)
    println("foo")
end

function foo{T}(x::T)
    println("Method foo{T}")
end

and since foo{T}(x::T) is deemed more specific than foo(x), foo{T}(x::T) is called instead of foo(x).

It seems like a warning might be appropriate in this case.

In the above example, I get the same behaviour on both 0.5 and 0.6, using these versions:

julia> versioninfo()
Julia Version 0.5.1-pre+55
Commit 8d4ef37* (2017-02-13 09:11 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin16.4.0)
  CPU: Intel(R) Core(TM) i7-6567U CPU @ 3.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)
julia> versioninfo()
Julia Version 0.6.0-pre.alpha.5
Commit b1242df* (2017-02-28 18:33 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin16.4.0)
  CPU: Intel(R) Core(TM) i7-6567U CPU @ 3.30GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)

However, I get different behaviour depending on whether I call foo() in between the definitions.

In 0.5:

julia> function foo(x=Type)
           println("foo")
       end;

julia> foo()
foo

julia> function foo{T}(x::T)
           println("Method foo{T}")
       end;

julia> foo()
foo

In 0.6:

julia> function foo(x=Type)
           println("foo")
       end;

julia> foo()
foo

julia> function foo{T}(x::T)
           println("Method foo{T}")
       end;

julia> foo()
Method foo{T}
@JeffBezanson
Copy link
Sponsor Member

In 0.6, foo{T}(x::T) overwrites foo(x::Any), since they have identical applicability (the 0.5 type system did not know this, and allowed the definitions to coexist).

Other than that, this is #7357.

@yuyichao
Copy link
Contributor

foo{T}(::T) isn't more specific but it's a method overwrite which is correctly handled on 0.6.

and yes, Jeff beats me at finding #7357....

@vtjnash
Copy link
Sponsor Member

vtjnash commented Apr 21, 2017

close as duplicate. also #265

@spurll
Copy link
Contributor Author

spurll commented Apr 21, 2017

Thanks. @omus and I didn't find #7357 in our quick search of issues.

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

4 participants