Skip to content

Warn about function definitions with grouping parens #139

@c42f

Description

@c42f

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

# 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 (#116 (comment)) 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 #131 (comment)

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> 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> 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> function ((h(a::T)::T) where T)
           a
       end
h (generic function with 1 method)

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messagesBetter, more actionable diagnosticsintro issueGood issue to start contributing to JuliaSyntaxparser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions