On Thursday, 3 September 2015 at 16:46:30 UTC, Andrei Alexandrescu wrote:
http://stackoverflow.com/questions/32369114/leap-years-not-working-in-date-and-time-program-in-dlang

The gist of it is the user wrote =+ instead of +=. I wonder if we should disallow during tokenization the sequence "=", "+", whitespace. Surely it's not a formatting anyone would aim for, but instead a misspelling of +=.

Well, in this particular case, wouldn't it make more sense to simply make + as a unary operator illegal? It doesn't even _do_ anything, and I, for one, didn't even know that such an operator existed before this thread.

In general though, I think that warning based on whitespace is a slippery slope, especially when the language is designed such that whitespace doesn't matter unless it's required to make it so that a compound token is separate tokens. And this particular case isn't even generalizable, because there are other operators where they _would_ go in that order, and if you don't actually want to properly space out your operators with whitespace, then you'll end up with stuff like =-a and =*a which are perfectly legitimate. So, we're forced to either special case =+, or we're essentially going to require that you put a space after =, and while I think that everyone should, that's basically requiring folks to format their code in a particular way which isn't in line with D or its C/C++ ancestry and the expectations of the developers who use such languages.

So, honestly, I'm more inclined to tell folks that maybe if they want their code to be clear and avoid mistakes like this, they should put whitespace around operators, but it's a free world, and they're free to format their code in a way that's harder to read and more likely to help them shoot themselves in the foot if they really want to.

But regardless, I question that even allowing + as a unary operator even makes sense in the first place. I guess that some folks might use it to try and make the difference between a positive and a negative number more obvious? But I would have thought that it would make it _harder_ to distinguish them rather than easier. So, maybe we can just get rid of the unary + operator. But still, that would only help this particular case, and other operators which _do_ make sense as unary operators to the right of an assignment still have the potential of being mistyped to painful results if programmers don't actually put whitespace around = like most of us would.

- Jonathan M Davis

Reply via email to