Two questions hide under “does the language support Unicode?”, and most languages answer them in one tangle. What may the source contain? And how are strings stored at run time? Mica answers them separately, and both answers fit in a sentence. The source: anything — the language does not reserve the alphabet. The storage: a build-time choice between utf-32 and utf-8 that never changes what a program means.
The example is UtfSources; it pairs with Text, which owns the string value itself.
Identifiers in any script
var
Straße, Вулиця, Δρόμος, ストリート, 长变量名, सड़क : int32;
procedure 절차의긴이름();
begin
WriteLn(" a procedure named 절차의긴이름 answered %d", 42);
end;Identifiers in six scripts
Straße = 1, Вулиця = 2, Δρόμος = 3
ストリート + 长变量名 * Δρόμος = 19
सड़क = 6
Routine names too
a procedure named 절차의긴이름 answered 42A non-ASCII identifier is a name like any other — it declares, assigns, participates in expressions, names a procedure, and nothing about it is a special case to the compiler, the debugger, or the language server. A team in Kyiv, Kyoto or Kolkata writes its domain vocabulary in its own script, and the teaching examples on this site stay honest about what that costs: nothing.
A name follows Unicode’s identifier rule: it begins with a letter of any
script and goes on with the characters a word of that script is written
with. Hindi writes the dot below the ड of सड़क as a combining mark, and
Bengali and Thai write vowel signs above and below their letters; a name
holds each of those marks as a character of its own, so नमस्ते, নমস্কার
and สวัสดี are names as street is.
A name also has one spelling. An accented letter can be typed as one
character, é, or as the letter followed by its accent, and a screen shows
both alike; the compiler compares every name in Unicode’s Normalization Form
C, so the two spellings are one name, declared once and read either way.
Two invisible characters take a closer rule. The zero width joiner and the
zero width non-joiner draw nothing themselves; they ask for two characters
joined or kept apart, and some scripts need them to spell a word: Hindi writes
the half form क्ष with a joiner behind the virama, beside the conjunct
क्ष, and Persian keeps two letters of نامهای apart with a
non-joiner. A name holds a joiner exactly where its script needs one. A joiner
anywhere else would change nothing on the screen and make two names look
alike, so the compiler refuses it and names it:
the U+200D ZERO WIDTH JOINER stands in this name where no script needs it.
Strings count runes, whatever the build
Strings count runes, not bytes
こんにちは世界!🎌 — 9 runes
مرحبا بالعالم!🌍 — 15 runes
🚀 Mica 世界 — 9 runes
The walk takes rune-sized steps
'🚀 Mica 世界' walked in 9 steps, first 🚀, last 界Length answers what a reader would count on the screen. The rocket is
four bytes under utf-8 and one rune everywhere; the byte number would
change with the encoding, and that is exactly why the length of a Mica
string is not defined as one. The for ch in s walk takes the same nine
steps under either build — under utf-8 it advances a byte cursor by each
rune’s width in one pass, and none of that machinery reaches the program.
The choice, and the one place it shows
The encoding is a property of the build, never of a value:
make -C mica-container/examples/UtfSources run # utf-32, the default
make -C mica-container/examples/UtfSources clean
make -C mica-container/examples/UtfSources MICA_ENCODING=utf-8 # the same program?Almost. Every rune count, every walk, every comparison in this example is identical under both — but the second build refuses:
analyzer error 5184: format string validation error: string format
specifier at position 1 must use '%s' for UTF-8 encoding (found '%ls')A format specifier names the storage its argument arrives in — %ls for
utf-32 code units, %s for utf-8 bytes — and the compiler holds the format
string to the build’s truth rather than letting a mismatched specifier
garble a line at run time. That is the whole visible surface of the
encoding choice: one letter in format strings, checked before the program
exists.
When does utf-8 earn its flag? At the boundary. A program that mostly hands text to C libraries, files, or sockets saves the conversion by storing utf-8 natively — the C boundary examples build that way. A program that mostly indexes and slices its own text takes the default: utf-32’s fixed-width runes make positional access constant time.
What this does not do
- The encoding is never per-value. There is no utf-8 string next to a utf-32 string in one program, no tagged storage, no implicit transcoding between values — one build, one storage, by design.
- A
mica.projectcan pin it. The build flag and the project file are two spellings of the same fact; the editor’s diagnostics follow the project file, so what you see while typing is what the build does. - Look-alike letters stay apart. A Latin
aand a Cyrillicаare two different letters that look the same, so names spelled with them are two names. One spelling per name joins the spellings Unicode declares equivalent, not the ones that merely look alike.
Try it
make -C mica-container/examples/UtfSources run
make -C mica-container/examples/UtfSources clean
make -C mica-container/examples/UtfSources MICA_ENCODING=utf-8The third command meets refusal 5184 four times — once per %ls whose
argument would arrive as utf-8 bytes. Change those four specifiers to %s
and the program builds and prints the same runes; those one-letter edits
are the entire porting cost between the encodings.
Next
The strings group is told: the value, builder and window in Text, the source and the encoding here. The series continues with programs in the large — units, libraries, and generics.