I have a problem where a piece of code, which was copied from Slack (this seems to be the source of the error) into an existing Kotlin file, made the code unable to compile. At first it was just a matter of a missing import, but even after that had been taken care of, it still failed with
(22, 1): Expecting a top level declaration
After removing some blank lines, and some line feeds and replacing them with new blank lines and new line feeds, the code compiled. Now, I am lucky it only took me ten minutes to figure out, because this is something that could have taken hours, especially for someone new to Kotlin. I was so confident that the code was correct that I could quite quickly deduce that it was likely to be this sort of error, but a novice Kotlin programmer might have thought it was their code that was at fault.
So I have some questions
- How can I prevent this from happening again?
- Can I somehow display line feeds in Intellij, and what sort of line feeds it is? (I recall having this option before, but I cannot find it any more)
- Is it possible to show an error in Intellij when any other sort of whitespace other than the “correct” whitespace (
U+0020
) is present? The compiler gave an error, which was indicated in the editor, but it was very hard to see since it wasn’t any character to display the error one. - Does someone have a git commit hook to prevent this sort of error and preventing a bad character being committed into the code base?
- Is it possible to improve the error message for this kind of compilation error?
I can’t include the entire code, but I can include the patch that fixed the problem. While adding the patch to GitHub Gists, GitHub noticed and pointed out that there was a non standard white space in it. It appears that the culprit was a zero width space (U+200B
).