Why is this a "malformed expression"?

julia> import Base: first

julia> (first((t,)), b) = 3, 5
ERROR: syntax: malformed expression
Stacktrace:
 [1] top-level scope
   @ REPL[23]:1

julia> first((t,)) = 3; #DON'T DO THIS, MESSES UP REPL DISPLAY

julia> (first(t), b) = 3, 5;  #DON'T DO THIS, MESSES UP REPL DISPLAY

The last two lines do not produce any error (though redefining first like this messes up the REPL for the rest of the session), but the one previous to them, (first((t,)), b) = 3, 5 leads to a “malformed expression” error.

(I came across this while looking into Issue#46482. It’s somewhat orthogonal to the original issue mentioned, and there may be a reasonable explanation I’m missing, so I’m posting it here.)

1 Like

Well, it’s definitely not surprising that these expressions are malformed. The SYSTEM (REPL): showing an error caused an error errors seem like something that should be reported to the issue tracker, though.

SYSTEM (REPL): showing an error caused an error is not really surprising either. Redefining the method Base.first(t::Tuple) is type piracy. first is used multiple times in the source code of the REPL so it is not surprising that redefining a method will cause the REPL malfunctioning.

1 Like

Another issue raised by this post: Is it OK to use multiple LHS assignment when defining one-line functions? It seems that Julia (1.7.1) is inconsistent on this matter:

julia> f(x),g(x)=3,5;

julia> f(12) + g('a')
8

julia> f(x),g(x)=x*x,x*x*x
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ REPL[22]:1

Maybe this deserves a second issue?

2 Likes

Interesting find, seems like it deserves a Github issue.

Yeah, this part is not surprising to me; I mentioned it just as a warning for people here who might want to try it out in their existing sessions (and end up with a messed up REPL).

Perhaps avoiding first and using a different example would make things clearer:

julia> foo((t, )), b = 3, 5
ERROR: syntax: malformed expression
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

julia> foo((t, )) = 3
foo (generic function with 1 method)

julia> foo(t), b = 3, 5
(3, 5)

This is sort of what I was getting at as the main issue in the post, I just didn’t word it clearly enough. Specifically, it seems like it should be ok, since foo(t), b = 3, 5 works, but foo((t, )), b = 3, 5, which should be the same thing but with a tuple-destructuring argument, leads to a malformed expression error.

1 Like

OK, I opened an issue: Disallow multiple LHS assignment for one-line function definitions · Issue #46517 · JuliaLang/julia · GitHub

1 Like