Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric keys disallowed in object expressions #3352

Open
gwhitney opened this issue Jan 17, 2025 · 0 comments
Open

Numeric keys disallowed in object expressions #3352

gwhitney opened this issue Jan 17, 2025 · 0 comments
Labels
category:expressions Issues about the expression parser, variable scoping etc. feature

Comments

@gwhitney
Copy link
Collaborator

Describe the bug
Unlike in JavaScript, numeric literals cannot be used as plain object keys.

To Reproduce
math.evaluate('{0: 12-7, 1: 8+2}[2-2]') throws SyntaxError: Symbol or string expected as object key (char 2).

Discussion
It appears that the parser knows exactly what the situation is, which suggests there is no technical reason why this expression couldn't be allowed in the mathjs expression language. Given that it is perfectly legal in JavaScript and evaluates to 5 as expected, I would recommend that the mathjs parser be extended to accept numeric literals as object keys.

In order for this to be useful, the mathjs evaluator will also need to be extended to allow numeric values in indexing into plain objects: currently math.evaluate('{"0": 12-7, "1": 8+2}[2-2]') throws TypeError: Cannot apply a numeric index as object property

Workaround for now
One can convert both the keys and the index expressions to strings: math.evaluate('{"0": 12-7, "1": 8+2}[string(2-2)]') produces 5 as expected. But this is of course cumbersome.

@gwhitney gwhitney added feature category:expressions Issues about the expression parser, variable scoping etc. labels Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:expressions Issues about the expression parser, variable scoping etc. feature
Projects
None yet
Development

No branches or pull requests

1 participant