5 ms·
since basically no other language with dot syntax lets you do myRecord . someField with a space in between, it doesn't seem like something that will actually tr
by platz 6y ago
since basically no other language with dot syntax lets you do myRecord . someField with a space in between, it doesn't seem like something that will actually trip newbies up
- sanxiyn 6y agoHuh? It works in C (I just tried) and I bet in most other languages.
- agency 6y agoWorks in JS as well. Though I have never seen anyone put whitespace around field accesses like that, in any language.
- taeric 6y agoNewlines are pretty common. Especially in chained cases. No?
- platz 6y agoi guess i forgot about the newlines case, and just had never seen anyone put spaces in like that without newlines, so figured it didn't exist. strange that it's even allowed.
- codeflo 6y agoThat’s because C-like languages don’t distinguish newlines from other whitespace. If you want foo() .filter(...) .map(...) then you have to allow foo() .filter(...) .map(...) as well, as a matter of principle.
- eru 6y agoYes. Though even Python, which does treat newlines different from other whitespace, allows `object . someMethod` with spaces on either side.
- squiggleblaz 6y agoBut this doesn't apply to Javascript in its absolute sense, since although (1) foo() .bar() is equivalent to (2) foo() .bar() there is a difference between (3) foo() bar() and (4) foo() bar() (the (3) results in valid code, whereas (4) would not). But trying to argue that (2) should be syntactically invalid would be a very hard ask indeed.
- eru 6y agoWorks in Python, too.
- klodolph 6y agoThe spaces are mandatory in Python, no? >>> 2020 . to_bytes(4, 'little') b'\xe4\x07\x00\x00' >>> 2020.to_bytes(4, 'little') File "<stdin>", line 1 2020.to_bytes(4, 'little') ^ SyntaxError: invalid syntax JavaScript also requires the spaces: > 2020 . toFixed(2) '2020.00' > 2020.toFixed(2) 2020.toFixed(2) ^^^^^ Uncaught SyntaxError: Invalid or unexpected token
- goto11 6y agoNumeric literals is a special case (in most languages), since the dot could also be a decimal separator. You have to use space or parentheses to disambiguate. But for decimals there is no ambiguity: >>> 2020.50.is_integer() False
- klodolph 6y agoYou say disambiguate, but there is no ambiguity. Only one parse could possibly be valid.
- foldr 6y agoIt's a real lexing ambiguity because '0.' is a valid numeric literal (so '0..toString()' parses ok, somewhat counterintuitively). In principle, yes, you could lex '.' as a separate token even in numeric literals and have the parser figure it all out.
- klodolph 6y agoNo, it is not a lexing ambiguity. You just need one extra character of lookahead after encountering a decimal point.
- goto11 6y agoFor languages with C derived syntax, whitespace around operations are insignificant. "a+b" is the same as "a + b". But by convention, space after dot is very rare.
- samatman 6y agoWorks in Lua. I even use it sometimes, like this: local field = require "some/module" . field When I want exactly one field from a module which returns a table. I think the spaces look nicer next to the string.
- renox 6y agosigh don't forget the /s next time..
- platz 6y agoi dont care