single quotes with characters in swift -


i have done c, c++, java , these language taught me characters enclosed in single quotes(mostly when abiding proper syntax) strings double quoted. swift's syntax allows characters inside of single quotes or there valid reason(logic) behind offering kind of syntax.

let char1: character = "a" //correct   let char2: character = 'b' //incorrect  

the state of compiler technology has changed great deal since time first c compiler developed. compilers became lot smarter figuring out things on own, including intended type of expressions, without programmers.

figuring out char vs. string literals 1 such example. theoretically, structure of today's c allows infer type of literal in many contexts. example, in code below compiler has enough information treat single-character strings if character literals:

void foo(char c); char s[] = "xyz"; // none of below compile char = "a"; foo("b"); if (s[1] == "c") {     ... } 

back @ time, however, easier ask programmer tell compiler "a", "b" , "c" 'a', 'b', , 'c'. moreover, since function prototypes not introduced until ansi c, foo("b") inference not possible in original k&r version of language.

programmer's no longer required when language has type inference system, swift designers decided unify syntax string , character constants.


Comments