Interpolation
Before this: Remembering Values
Next steps: Fmt
Most text a circuit shows mixes fixed words with live values: a score, a
count, a name. ${} puts a value into a piece of text. This lesson shows what
it compiles to, and why it is cheaper than joining the pieces yourself.
Contents
chip2 "Counter"
chip "Sign"
button1 Held -> chip2 addRed
button2 Held -> chip2 addBlue
chip2 red -> chip red
chip2 blue -> chip blue
chip joined -> decal1 Text
chip templated -> decal2 Text
Code
@left in red: int
@left in blue: int
@right out joined = "Red " .. red .. ", Blue " .. blue .. ", Total " .. (red + blue)
@right out templated = "Red ${red}, Blue ${blue}, Total ${red + blue}"
Text in quotes is a string. .. joins two strings into one, and turns a
number into text on the way. ${...} inside a string puts the value of what
is between the braces at that spot. The text around it stays as written. A
string with ${} in it is called a template.
Both outputs build the same words. Which one takes fewer gates? Guess a number for each before you open the chip.
Run
-
Compile the chip and paste it in-game. Place two Buttons and two bricks with a Text Decal.
-
Paste this counter chip as well. It counts each button’s presses, the way Remembering Values does, and puts the two counts on pins. The next lessons in this branch use it too.
@left in addRed: exec @left in addBlue: exec var redCount: int = 0 var blueCount: int = 0 @right out red = redCount.Value @right out blue = blueCount.Value on addRed { redCount += 1 } on addBlue { blueCount += 1 } -
Wire the first Button’s Held to the counter’s
addRedand the second’s toaddBlue. Wire the counter’sredandblueto the first chip’sredandblue. -
Wire
joinedto one Text Decal’s Text andtemplatedto the other’s. -
Press each button a few times. The two signs always read the same.
Under the Hood
Open the first chip and count the gates.
joinedis five Append String gates in a row. Each..is one gate, and each gate joins exactly two pieces. Three values with text in front of each make six pieces, and six pieces take five joins. In general, N values with text in front of each cost 2N-1 gates.templatedis one Format String gate. Its Format String isRed {0}, Blue {1}, Total {2}: each${}became a numbered slot, and the values are wired into the gate’s Input A, Input B and Input C.red + blueis one Add gate, and both outputs share it.
A Format String gate has seven inputs, Input A to Input G, so one gate holds up to seven values. Write more and the compiler splits the template across several gates and joins their outputs, so you never have to count:
@left in n: int
@right out row = "${n} ${n - 1} ${n - 2} ${n - 3} ${n - 4} ${n - 5} ${n - 6} ${n - 7} ${n - 8}"
This row has nine values. Open the chip: there are three Format String gates, one with seven slots, one with two, and one that joins the first two.
A brace in the text
The Format String gate reads {0} as a slot, so braces mean something to it.
In a ${} template you can still type { and } as text: the compiler
doubles each one in the gate’s text so the gate does not take it for a slot.
@left in red: int
@right out sign = "{red} = ${red}"
Open the chip: the gate’s Format String is {{red}} = {0}. Wire sign to
a Text Decal and check that it shows single braces. To show a $, write \$.
Modify
-
Add who is ahead.
if ... then ... elsepicks one of two values: here"Red"whenred > blueis true,"Blue"when it is false. Give it a name withlet, then put the name in the template.@left in red: int @left in blue: int let leader = if red > blue then "Red" else "Blue" @right out text = "Red ${red}, Blue ${blue}. ${leader} leads."What does the sign say when the scores are equal?
-
Fix the tie: an
elsecan hold anotherif.if red == blue then "Nobody" else if red > blue then "Red" else "Blue". -
Put the two scores on separate lines with
<br>, and color the names with<color="RRGGBB">(Rich Text Markup). Write the template in single quotes so the double quotes in the tag need no escaping:'<color="ff4040">Red ${red}<br><color="4080ff">Blue ${blue}'. It is still one gate: tags are just more text.
Make
Make a sign for three teams that shows each score and the total, written as
one template. How many gates does it take? How many would the .. version
take?
button1 Held -> chip red
button2 Held -> chip blue
button3 Held -> chip green
chip text -> decal Text
Good to Know
- A
boolin a slot prints as a number:trueshows1. Word Forms prints words instead.
See String Interpolation for the reference.
Next: Fmt.