I was writing a small console application in Delphi (XE), and by mistake wrote:
for I := 0to aList.Count-1 do
Note the missing space between "0" and "to"
I didn't notice this until after I had run the program, and I was surprised the compiler accepted this. It's probably no big deal, but it made me curious.
Why does Delphi accept this typo?
Most whitespace characters are ignored, not all of them are. In the earlier example one of the spaces between "Hello" and "World!" still exists when the page is rendered in a browser.
You can also solve this problem by putting your list items all on the same line in the source, which causes the whitespace nodes to not be created in the first place: When trying to do DOM manipulation in JavaScript, you can also encounter problems because of whitespace nodes.
The presence of whitespace in the DOM can cause layout problems and make manipulation of the content tree difficult in unexpected ways, depending on where it is located. This article explores when difficulties can occur, and looks at what can be done to mitigate resulting problems. What is whitespace?
It's for the same reason that you don't need spaces around the .
or the -
. Since a t
can never come after an initial 0
in any recognisable token, the lexical analyser simply returns the 0
as an integer-literal token, and then recognises the to
as a distinct keyword token. If you had made a different mistake instead — for I := 0 to10
— you'd now have a problem, since to10
is recognised as a valid identifier, which is illegal immediately after the 0
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With