JuliaLang / JuliaLang/JuliaSyntax.jl
Warn about function definitions with grouping parens
- Dominant language
- Julia
- Stars
- 293
- Forks
- 50
- PR merge metrics
- No merged PRs in 30d
Description
Function definitions with grouping parentheses around the signature are ugly and somewhat syntactically ambiguous. We must support them for compatibility but I think they're a syntactic misfeature we should discourage with a warning (and eventually in the far future disallow).
---
A first example of ambiguity
```julia
# 0-arg function named `f`
function (f())
end
# 1-arg anonymous function
function (f)
end
# function declaration without methods
function f
end
```
Arguably, the first form could be seen as a 1-arg anonymous function (with invalid nested expression `f()` which would be caught at lowering).
Allowing grouping parentheses here isn't ever necessary but has been done several times in the package ecosystem. The only valid use case I can think of is wanting to put the `where` on a new line (https://github.com/JuliaLang/JuliaSyntax.jl/issues/116#issuecomment-1271096904) but this is not really necessary.
---
There's a further problem about the precedence of `::` and `where` sufficies to the call expression. To copy some comments from https://github.com/JuliaLang/JuliaSyntax.jl/pull/131#issuecomment-1295364086
You can't "just add grouping parens" without changing the relative precedence of `::` and `where`:
Ie, we can't go from the normal syntax
```julia
julia> function f(a::T)::T where T
a
end
f (generic function with 1 method)
julia> f(1)
1
```
To the same thing with parens without the precedence of `::` and `where` changing:
```julia
julia> function (g(a::T)::T where T)
a
end
ERROR: UndefVarError: T not defined
```
Additional parens (like in your example) are required to manually fix the precedence:
```julia
julia> function ((h(a::T)::T) where T)
a
end
h (generic function with 1 method)
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.