julia icon indicating copy to clipboard operation
julia copied to clipboard

Attempting to assign to a variable starting with a number leads to cryptic "function argument" error

Open BioTurboNick opened this issue 4 years ago • 4 comments

2x = 5
#ERROR: syntax: "2" is not a valid function argument name

This is a sneaky problem. A novice user doesn't know (and even an intermediate user might puzzle a bit): 1) numbers cannot be used to start names 2) a number juxtaposed with a name is equivalent to *(2, x) 3) *(2, x) on the left-hand-side doesn't perform the operation, but is a function signature 4) therefore, 2x = 5 is attempting to define *(2, x) = 5, and 2 is not a valid function argument name.

Issue brought up in Slack thanks to Yongming Han.

I'm willing to poke around a bit on this one and see what could be done.

BioTurboNick avatar Apr 05 '21 13:04 BioTurboNick

Thanks for making this issue; I'm pretty swamped for the next couple of weeks, but would be down to help out after that if there's anything a relative novice like me can help with.

ym-han avatar Apr 05 '21 22:04 ym-han

Thanks for the offer @ym-han ! Looks like it was pretty easy to figure out, though mostly because I've recently gotten my hands dirty with the femptolisp-based parser.

For posterity: Other invalid left-hand expressions can lead to the same error message, which are still somewhat cryptic but hopefully clearer to the user why it's invalid, regardless. E.g.:

1 + x = 5
# ERROR: syntax: "1" is not a valid function argument name

1x * 4 = 3
# ERROR: syntax: "(1 * x)" is not a valid function argument name

Maybe something to clean up later?

BioTurboNick avatar Apr 06 '21 03:04 BioTurboNick

@ym-han Would you find this good enough to understand what was happening? Unfortunately it seems like it won't be possible to catch it early enough to be sure someone is writing something like 2x = specifically, so I'm thinking this should at least more clearly indicate how Julia is interpreting 2x =?

julia> 2x = 5
ERROR: syntax: invalid function definition; "2" is not a valid function argument name in "(2 * x)"

BioTurboNick avatar Apr 07 '21 01:04 BioTurboNick

This looks good enough to me. Thanks for the heroic effort here!!

ym-han avatar Apr 07 '21 01:04 ym-han