In java or in scala declaring a string within single quotes would throw the
‘error: unclosed character literal’
I was wondering if anybody can tell me the exact mechanics of how the string within the single quotes is type checked by the compiler and then the determination of the error is made. Also, why does the compiler say ‘
unclosed character literal
‘ instead of
“String instead of char”
as the error description?
Thanks
This is usually done by a lexical analyser. As the wikipedia article says, that is usually done by a finite-state machine, which are very fast and easy to generate.
As for why it displays “unclosed character literal” instead of “string instead of char” is because the way languages are usually parsed (a different step than lexical analysis) makes it much harder for compilers to realize what the programmer intended than for a person looking at it. The context used by the compiler is usually what is to the left and above the problematic part, instead of the whole context.
So, looking to the left of the opening quote, it is impossible to tell whether the error is a missing single quote, or whether the programmer mixed single quotes with double quotes. This is compounded in Scala by the syntax for symbols, (
'aSymbol), which prevents it using the very next character as a way to distinguish one from the other.So, in the end, the error message is picked to reflect what is the most likely error, and, for anyone coming from Java, C or C++, using single quotes around strings is just not an habit. For people coming from scripting languages such as Ruby, Perl or Python, that’s another story.