julia icon indicating copy to clipboard operation
julia copied to clipboard

Underscore not allowed with optional parameters

Open cstjean opened this issue 6 years ago • 5 comments

julia> f(_, a) = 10
f (generic function with 1 method)

julia> f(_, a=1) = 10
ERROR: syntax: all-underscore identifier used as rvalue around REPL[0]:1

The second form seems legitimate, no? The same issue arises with keyword arguments (f(_; x=1) = ...)

cstjean avatar Jul 29 '19 19:07 cstjean

It does seem like a good way to indicate that you don't care about an argument, similar to ::Any but shorter.

StefanKarpinski avatar Jul 29 '19 20:07 StefanKarpinski

Ah, naturally this is due to lowering converting the definition to

f(_) = f(_, 1)
f(_, a) = 10

JeffBezanson avatar Jul 29 '19 21:07 JeffBezanson

Could be nice to lower _ as a function argument to @nospecialize(_)

quinnj avatar Jul 29 '19 23:07 quinnj

how does the lowering for keyword arguments look like? is it the same reason there?

schlichtanders avatar Nov 16 '19 09:11 schlichtanders

LanguageServer.jl now offers a quickfix code action for replacing unused argument with _ (https://github.com/julia-vscode/LanguageServer.jl/pull/1072), but then you run into this issue pretty often.

fredrikekre avatar May 17 '22 12:05 fredrikekre