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

**URL:** <https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [June 17, 2021, 2:24pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092 "2021-06-17T14:24:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 2:24pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/1 "2021-06-17T14:24:41Z")

</div>

Hi all 🙂

```julia
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
julia> f(g = (::Dict{Any, Any}) -> begin end) = g
f (generic function with 2 methods)

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 2:34pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/3 "2021-06-17T14:34:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 17, 2021, 2:43pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/4 "2021-06-17T14:43:01Z")

</div>

Filed an issue: [parse error from type T{} in anonymous function · Issue #41253 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/41253)

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

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

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 3:09pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/5 "2021-06-17T15:09:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 3:16pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/6 "2021-06-17T15:16:28Z")

</div>

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

```julia
"""
   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.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 3:17pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/7 "2021-06-17T15:17:40Z")

</div>

That doesn’t work as well…

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 3:18pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/8 "2021-06-17T15:18:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 3:21pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/9 "2021-06-17T15:21:36Z")

</div>

You’re welcome! You’ll also get e.g. [Documenter.jl](https://juliahub.com/ui/Packages/Documenter/sHGjf/0.27.1) 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.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 20, 2021, 12:43pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/10 "2021-06-20T12:43:29Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [July 1, 2021, 11:47am UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/11 "2021-07-01T11:47:05Z")

</div>

This is still broken!

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [July 1, 2021, 11:58am UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/12 "2021-07-01T11:58:07Z")

</div>

```julia
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"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 1, 2021, 12:33pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/13 "2021-07-01T12:33:23Z")

</div>

You should file a separate issue.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [July 1, 2021, 12:55pm UTC](https://discourse.julialang.org/t/why-cant-i-use-in-a-default-function-definition-seems-like-a-bug/63092/14 "2021-07-01T12:55:37Z")

</div>

OK I will 🙂
