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

Splatting creates spurious warning about extending function without using module defined typed arguments #224

Closed
CameronBieganek opened this issue Nov 27, 2020 · 5 comments

Comments

@CameronBieganek
Copy link

image

image

image

@pfitzseb
Copy link
Member

At least the first case is valid, no? You define Base.cat(us::Union{}; dims::Integer) there, which looks like piracy to me.
Dunno where the types come from in the second case, so maybe that actually is a bug.

@CameronBieganek
Copy link
Author

CameronBieganek commented Nov 27, 2020

You define Base.cat(us::Union{}; dims::Integer) there

Hmm, I'm not following you. The signature in that function definition is

Base.cat(us::UnivariateFiniteArray{S,V,R,P,N}...; dims::Integer) where {S,V,R,P,N}

Notice that in the second screen capture, the warning goes away after removing the splatting.

That code is from MLJBase.jl. Here's a link.

Here's a reproducible example:

struct A
    x::Int
end

Base.cat(a::A...; dims::Integer) = a.x + 1

(I deleted the second example since I can't reproduce it.)

EDIT: Sorry I was too lazy last night to add explanatory notes to the screenshots and come up with a reprex. 😂

@pfitzseb
Copy link
Member

You're defining Base.cat for zero arguments as well with that definition:

julia> f(xs::Int...) = xs
f (generic function with 1 method)

julia> f()
()

julia> f(1)
(1,)

julia> f(1,2)
(1, 2)

julia> f(xs::Float64...) = "float"
f (generic function with 2 methods)

julia> f()
ERROR: MethodError: f() is ambiguous. Candidates:
  f(xs::Int64...) in Main at REPL[1]:1
  f(xs::Float64...) in Main at REPL[9]:1
Possible fix, define
  f()

@pfitzseb
Copy link
Member

pfitzseb commented Nov 27, 2020

Oh, and the usual fix for this is to define f(x::Int, xs::Int...) instead (although you might be able to do something smarter).

@CameronBieganek
Copy link
Author

Oh, ok, makes sense. I didn't write that code, but I'll make a PR to MLJBase.

Although it does seem a little odd to me that foo(args...) also defines the foo() case. It seems to me that the costs of including the zero argument case in a vararg definition are greater than the benefits.

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

2 participants