5 ms·
First off, you're plain wrong about Python, so I doubt the veracity of the other examples (particularly in languages I'm not an expert in, like Ruby). But eithe
by dvt 5y ago
First off, you're plain wrong about Python, so I doubt the veracity of the other examples (particularly in languages I'm not an expert in, like Ruby). But either way, you (along with the other folks commenting) are missing the point entirely, so I'm not sure you're actually familiar with the language spec nit I'm criticizing.
Why `console.log` fails on `((...x))` is not because the function can't take arrays as arguments (in fact, it can: `console.log(["hello","world"])` works fine), but rather because it's running into another spec: inline arrow function notation, i.e., `(...x) => true`. So the spec (because it's so confusing) thinks I'm trying to define a function.
That is the problem here, not the splat.
> You wouldn't change console.log(a, b) to console.log((a, b)) and expect it to work the same, would you?
Honest question, are you aware of the comma operator and how it works?
- minitech 5y ago> First off, you're plain wrong about Python They’re not. The syntax was changed sometime between Python 3.8 and 3.10 to stop working (which should be a big hint about the consensus here). As I suspected, that it worked was accidental from the very beginning: https://bugs.python.org/issue40631#msg384094 https://bugs.python.org/issue40631#msg384094 > Why `console.log` fails on `((...x))` is not because the function can't take arrays as arguments (in fact, it can: `console.log(["hello","world"])` works fine), but rather because it's running into another spec: inline arrow function notation, i.e., `(...x) => true`. So the spec (because it's so confusing) thinks I'm trying to define a function. What does taking arrays as arguments have to do with anything? Are you suggesting `console.log((...x))` should be equivalent to `console.log([...x])`? `(...x)` doesn’t fail to work because some kind of syntax conflict with arrow functions[1]. It fails to work because it’s not defined in the language, and it’s not defined in the language because it doesn’t make sense. You’re the only person who seems to think it does. And if you want to use parentheses for readability, you should write `...(expr)`, `*(expr)`, etcetera. [1] f((a = {})) and f((a = {}) => {}), for example, are both valid expressions where the inner parentheses mean completely different things, analogous to the ellipsis.
- abbeyj 5y ago> First off, you're plain wrong about Python As pointed out by minitech, this syntax was allowed in some versions of Python 3 but that was a bug which has since been fixed. I didn't run into this bug in the version of Python 3 that I used for testing so I had no idea of the bug's existence. > I doubt the veracity of the other examples Python 3.10: https://godbolt.org/z/Po7K7cvY8 https://godbolt.org/z/Po7K7cvY8 Ruby 3.0.2: https://godbolt.org/z/Ke5oWzaW3 https://godbolt.org/z/Ke5oWzaW3 C++ (clang 13.0.0): https://godbolt.org/z/zd5MfM4Mo https://godbolt.org/z/zd5MfM4Mo Go (amd64 gc 1.17): https://godbolt.org/z/KrEsKaTjq https://godbolt.org/z/KrEsKaTjq Lua (luac 5.3.3): https://ideone.com/irl89j https://ideone.com/irl89j Perl (perl 2018.12): https://ideone.com/YZcCAC https://ideone.com/YZcCAC > Honest question, are you aware of the comma operator and how it works? I am. The existence of the comma operator is the reason that `console.log((a, b))` parses (and means something different than `console.log(a, b)`). If we consider a hypothetical version of JavaScript which does not have the comma operator then `console.log((a, b))` wouldn't parse at all! The comma there can't be the comma operator because we've removed that. And it can't be part of the function call because then the first argument to the function call would have to be `(a` which is obviously not a valid expression. And it can't be part of an array literal or an object literal because it isn't inside of `[]` or `{}`. That leaves us with no valid ways for this to parse. You can usually add extra parentheses around any expression without changing the meaning. But "all the arguments to a function" is *not* an expression (except in the case where there happens to be exactly one argument). This is why it doesn't make sense to change `console.log(a, b)` into `console.log((a, b))`, why it doesn't make sense to change `console.log()` into `console.log(())`, and why it doesn't make sense to change `console.log(...a)` into `console.log((...a))`.