JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.48k stars 5.46k forks source link

Whitespace not allowed after function signature #54085

Open jariji opened 5 months ago

jariji commented 5 months ago

I don't think this should be an error.

julia> function foo(x) x end
foo (generic function with 3 methods)

julia> function foo(x) (x) end
ERROR: ParseError:
# Error @ REPL[20]:1:16
function foo(x) (x) end
#              ╙ ── whitespace is not allowed here

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 24 × AMD Ryzen 9 3900XT 12-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 17 on 24 virtual cores
Environment:
  JULIA_NUM_THREADS = 12
Keno commented 5 months ago

The parse of the second is function foo(x)(x) end, which was deemed likely not what you want in many situations, so the error was added intentionally to prevent people from getting the wrong thing accidentally.

jariji commented 5 months ago

function foo(x)(x) end tries to define a function named foo(x) but that doesn't seem like right parse for function foo(x) (x) end which I would expect to be the same as function foo(x) x end.

Keno commented 5 months ago

which I would expect to be the same

Yes and you're not the only, which is why this was made an error ;). More fundamentally, the parsing rules for signatures are the same as the parsing rules for regular calls. That could be changed of course, but that's a much more breaking (and also backwards incompatible) change. You can use a semicolon though: function foo(x); (x) end