String Interpolation
Fill-in-the-Blanks
Imagine a birthday card template: "Happy Birthday, ____! You are ____ years old." You fill in the blanks with the right name and age.
String interpolation is Pebble's version of fill-in-the-blanks.
Instead of joining pieces together with +, you put expressions right
inside the string using curly braces {…} and Pebble fills them in for
you.
How It Works
Wrap any expression in { and } inside a double-quoted string:
Pebble evaluates the expression, converts the result to text, and slots it into the string at that position.
Multiple Values
You can have as many {…} sections as you like in one string:
Any Expression Works
The bit inside the braces can be any Pebble expression — a variable, a number, a calculation, even a function call:
Values are automatically turned into text:
| Type | Example | Text |
|---|---|---|
| Integer | {42} |
42 |
| Float | {3.14} |
3.14 |
| String | {"hi"} |
hi |
| Boolean | {true} |
true |
Literal Braces
If you actually want a { character in your string (not interpolation),
put a backslash before it:
The \{ is one of several escape sequences Pebble supports. See the
escape sequences section in the strings
docs for the full list — including \n (newline), \t (tab), \\
(literal backslash), and more.
How It Works Under the Hood
When the lexer sees { inside a string, it splits the string into
segments:
becomes the token sequence:
The parser collects these into a StringInterpolation node. The
compiler emits code to push each part onto the stack, then a
BUILD_STRING instruction that pops them all and joins them into one
string.
Think of it like making a sandwich: each ingredient is stacked up, then
BUILD_STRING squashes them all together into one finished sandwich.