Lexical
Char literals
A single-quoted literal becomes a char when the context expects one and it holds exactly one Unicode scalar.
since 0.1.0-alpha.1linuxwindowswasm
Description
The language has no separate char literal syntax. A single-quoted literal is a string by default, exactly like a double-quoted one. It is re-typed to char when two things are true:
- the type expected at that point is
char, and - the decoded literal is exactly one Unicode scalar.
char c = 'a'; is a char. string s = 'a'; is a string. A double-quoted literal is never a char.
Escapes work in char literals: '\n', '\t', '\x41' and '\u{1F600}' are all single scalars.
Char literals in typed contexts
use io::console;
char c = 'a';
char tab = '\t';
char hex = '\x41';
char smile = '\u{1F600}';
console.writeln(c.code());
console.writeln(tab.code());
console.writeln(hex);
console.writeln(smile.code());
console.writeln(c == 'a');
console.writeln(c < 'b');
97
9
A
128512
true
true
Rules
- A
charholds one Unicode scalar (0to0x10FFFF, surrogates excluded). A declaredcharthat is never assigned holds scalar0. - A single-quoted literal becomes a
charin these contexts: a declaration or an assignment whose type ischar(orchar?), a comparison against acharoperand (c == 'a'), amatcharm over acharsubject, areturnin a function or lambda that returnschar(orchar?), an argument passed to achar(orchar?) parameter, including a call through a function-typed variable, field or parameter, the index of aMapwhose key type ischar(orchar?), and the index of an indexer that takes achar(orchar?) when no indexer of the receiver takes astring. At an assignment or a comparison, a union that can hold astring, such asstring | char, keeps the literal astring. An overload takingstring?is chosen over one takingchar?, in either declaration order. - The literal must hold exactly one scalar.
char c = 'ab';andchar c = '';are compile errors ("cannot initialize 'c' with 'string'").char c = "a";is also an error. - A literal compared against a
stringstays astring.string s = 'a'; s == 'a'compares two strings. - When both
f(char)andf(string)exist, a bare literal argument always choosesf(string). To call thecharoverload, pass a value that already has typechar. - A triple-quoted literal is always a
string, even with one scalar inside. ==,!=,<,<=,>and>=compare chars by scalar value. There is no arithmetic onchar; convert withcode()first.
Examples
Target typing in a return, in a match arm and in an overloaded call:
Where a literal becomes a char
use io::console;
char grade(int score) {
if (score >= 90) {
return 'A';
}
return 'F';
}
void classify(char x) {
match (x) {
'a' => console.writeln("the letter a");
else => console.writeln("something else");
}
}
void describe(char x) {
console.writeln("char overload");
}
void describe(string x) {
console.writeln("string overload");
}
console.writeln(grade(95));
classify('a');
classify('z');
describe('x');
char c = 'x';
describe(c);
describe("x");
A
the letter a
something else
string overload
char overload
string overload
A declared char with no initializer holds the scalar 0:
use io::console;
char blank;
console.writeln(blank.code());
The program prints 0. The compiled backend currently prints an empty line instead.
An inferred variable stays a string, because nothing asks for a char:
A literal with no char context is a string
use io::console;
void show() {
var s = 'a';
console.writeln(s.length());
string t = 'a';
console.writeln(t == 'a');
}
show();
1
true
Notes
The methods of char, such as code() and isDigit(), are documented in std.char.
See also
- char — A single Unicode character, stored as one code point.
- String literals — Quoted strings, escapes,
${}interpolation, rawr"..."strings and triple-quoted multiline strings.