The state of the compiler technology has changed a great deal since the time the first C compiler was developed. Compilers became a lot smarter about figuring out things on their own, including the intended type of expressions, without help from programmers.
Figuring out char
vs. string literals is one such example. Theoretically, the structure of today's C allows to infer the type of a literal in many contexts. For example, in the code below the compiler has enough information to treat single-character strings as if they were character literals:
void foo(char c);
char s[] = "xyz";
// None of the below would compile
char a = "a";
foo("b");
if (s[1] == "c") {
...
}
Back at the time, however, it was easier to ask the programmer to tell the compiler that "a"
, "b"
and "c"
are actually 'a'
, 'b'
, and 'c'
. Moreover, since function prototypes were not introduced until ANSI C, foo("b")
inference was not even possible in the original K&R version of the language.
Programmer's help is no longer required when the language has type inference system, so Swift designers decided to unify the syntax for string and character constants.
Character
s and especiallyString
s are complex beasts in Swift due to advanced Unicode grapheme/grapheme cluster handling