Why can't I use `{}` in a default function definition? Seems like a bug

Hi all :slight_smile:

julia> f(g = function(::Dict) end) = g
f (generic function with 2 methods)

julia> f(g = function(::Dict{}) end) = g
ERROR: syntax: expected "(" in function definition
Stacktrace:
 [1] top-level scope
   @ none:1

julia> f(g = function(::Dict{Any, Any}) end) = g
ERROR: syntax: expected "(" in function definition
Stacktrace:
 [1] top-level scope
   @ none:1

Why doesn’t it work as intended?

But:

julia> f(g = (::Dict{Any, Any}) -> begin end) = g
f (generic function with 2 methods)

That’s probably an issue with the parser, but I wouldn’t have expected this to work in the first place - it’s usually best to define that outside of the default argument and just pass the name of the function in.

Are you trying to restrict g to only match functions that take a dictionary as an argument? If so, that kind of dispatch doesn’t exist in julia, as far as I’m aware, since the types of each argument are not part of the type of a function.

1 Like

Filed an issue: parse error from type T{} in anonymous function · Issue #41253 · JuliaLang/julia · GitHub

The workaround is to use ::Dict, which is equivalent to ::Dict{}.

You can also use (::Dict{}) -> nothing as an alternative anonymous-function syntax.

2 Likes

It’s only for readability. Kind of an interface thing. So the user of my Pkg knows what kind of a function I expect.

I’d usually document this directly above f in its docstring:

"""
   f(g)

Combobulates `g` with internal difrabulation.

`g` is expected to take this signature: `g(::Dict{T,V}) where {T,V}`
"""
function f(g)
    ...
end

Also helps discovery when in help mode (help> f), since the function signature doesn’t show up there automatically but the docstring does.

That doesn’t work as well…

julia> f(a = (b::String)::Bool -> true) = a
ERROR: syntax: "b::String" is not a valid function argument name around REPL[13]:1
Stacktrace:
 [1] top-level scope
   @ REPL[13]:1

julia> f(a = (b::String)::Bool -> begin true end) = a
ERROR: syntax: "b::String" is not a valid function argument name around REPL[14]:1
Stacktrace:
 [1] top-level scope
   @ REPL[14]:1

Yes that’s probably better. I will try doing that. Seems better!

You’re welcome! You’ll also get e.g. Documenter.jl support for free, which already generates documentation from docstrings. It also has cross references and all kinds of fancy things you might be interested in.

1 Like

That’s on Julia 1.8-DEV. I don’t know if that can be fixed or if its ambigues…

julia> function()::Bool true end
ERROR: syntax: ambiguous signature in function definition. Try adding a comma if this is a 1-argument anonymous function.
Stacktrace:
 [1] top-level scope
   @ none:1

julia> ()::Bool -> true
#7 (generic function with 1 method)

julia> (::Bool)::Bool -> true
ERROR: syntax: "::Bool" is not a valid function argument name around REPL[12]:1
Stacktrace:
 [1] top-level scope
   @ REPL[12]:1

This is still broken!

julia> function(abc::Int) true end
#7 (generic function with 1 method)

julia> VERSION
v"1.7.0-beta2"

julia> function(abc::Int)::Bool true end
ERROR: syntax: expected "(" in function definition
Stacktrace:
 [1] top-level scope
   @ none:1
julia> function(a::Int)::Bool end
ERROR: syntax: ambiguous signature in function definition. Try adding a comma if this is a 1-argument anonymous function.
Stacktrace:
 [1] top-level scope
   @ none:1

julia> function(a::Int,)::Bool end
ERROR: syntax: ambiguous signature in function definition. Try adding a comma if this is a 1-argument anonymous function.
Stacktrace:
 [1] top-level scope
   @ none:1

julia> VERSION
v"1.8.0-DEV.94"

You should file a separate issue.

1 Like

OK I will :slight_smile:

2 Likes