Scene 1 The tree Twig brought
TwigAll done. This tree fits the grammar.
LineyIt looks just fine this time!
TwigThe shape does.Now it's Tami's turn.
The Life of Code 03
The tree looks fine, so why won't it work?
Twig put the pieces together and made a tree.
Now it's Tami's turn to look at what is inside the tree.
If the shape on the outside is right, does that mean every check is done?
So that's your guess.
Let's follow along and see what Tami looks at.
TwigAll done. This tree fits the grammar.
LineyIt looks just fine this time!
TwigThe shape does.Now it's Tami's turn.
LineyWhat are you checking now?
TamiTwig already checked the shape of the tree.I'm going to look at the values inside it.
LineyValues get checked too?
TamiOf course. You can't tell from the shape alone.
TamiThe left one is checked.
LineyWhat about the right?
TamiThat's next.
TamiHold on.
LineyWhat now? Twig said it was put together fine!
TamiIt was put together fine.Whether this plus can take this pair is a different question.
LineySo is this the end for me?
TamiNo. I'm going to tell you which part to fix.
LineyIf I fix it, can I come back?
TamiOf course. Checking is my job.
MomoThis one's all set.
What problem is written on Tami's card?
Let's check for ourselves.
A little experiment
It's the same plus, so why are the results different?
This time, you get to hold Tami's magnifying glass.
Look at both ends of the tree, and last of all, check the plus sign in the middle.
Hold the magnifying glass over the left and the right.
A tag will show up on each one, saying what it is.
1numberint
2numberint
Now take a look at the +.
Can this plus take both kinds together?
This pair is fine.
In this expression, the result of the plus is a number type too.
Resultnumberint
Topaz compiler 5.21.1 record
This record comes from the compiler of Topaz, the language this site runs its examples in.primitive intnumberintliteral int 1numberintliteral int 2numberintHold the magnifying glass over the left and the right.
A tag will show up on each one, saying what it is.
"apple"textstring
"pear"textstring
Now take a look at the +.
Can this plus take both kinds together?
This one is fine too.
In Topaz, you can join two strings together with +.
The type of the result is a string too.
Resulttextstring
Topaz compiler 5.21.1 record
This record comes from the compiler of Topaz, the language this site runs its examples in.primitive stringtextstringliteral string "apple"textstringliteral string "pear"textstringHold the magnifying glass over the left and the right.
A tag will show up on each one, saying what it is.
1numberint
"apple"textstring
Now take a look at the +.
Can this plus take both kinds together?
The plus can't take this pair as it is.
In Topaz, this number and this string can't be used together with +.
Look again
This expression left no type record. The tags show the kinds read from Topaz's rules.
Topaz compiler 5.21.1 record
TPZ5001error
`+` needs matching int, float, string, BigInt, or Decimal operands, found `int` and `string`
Points to cells 1 to 11
In plain wordsThe left is an integer and the right is a string. This plus doesn't take the two together.
Hold the magnifying glass over the left and the right.
A tag will show up on each one, saying what it is.
"1"textstring
2numberint
Now take a look at the +.
Can this plus take both kinds together?
Shall we look at the left again?
"1" looks like a number, but it is a string wrapped in quotes.
So 1 and "1" are not the same type.
Topaz can't add this expression as it is either.
Look again
This expression left no type record. The tags show the kinds read from Topaz's rules.
Topaz compiler 5.21.1 record
TPZ5001error
`+` needs matching int, float, string, BigInt, or Decimal operands, found `string` and `int`
Points to cells 1 to 7
In plain wordsThe left is a string and the right is an integer. This plus doesn't take the two together.
Samples left on the desk
1 + 2numberintnumberintAccepted"apple" + "pear"textstringtextstringAccepted1 + "apple"numberinttextstringLook again"1" + 2textstringnumberintLook again1 + 2numberintnumberintResultnumberint"apple" + "pear"textstringtextstringResulttextstringBoth use +, but the kinds of values being added are different.
This + is used to add numbers, and it is also used to join strings.
This time, we looked at both of those cases.
1numberint"1"textstringJust a pair of quotes can change the type.
The real inside story
When you looked over the tree with the magnifying glass, you were playing at a type check.
A compiler works out what type each expression in the code has, then checks whether an operation can use that type.
We met two types today.
intThe type for integers, which are whole numbers.
Values like 1, 2 and 4 belong here.
stringThe type for strings, which are pieces of text.
Letters wrapped in quotes, like "apple", "pear" and "1", belong here.
Topaz's + can add numbers, and it can also join strings.
That doesn't mean it takes any mix of kinds.
In this experiment, two numbers worked and two strings worked, but one number and one string could not be used together.
Topaz compiler 5.21.1 record
This record comes from the compiler of Topaz, the language this site runs its examples in.primitive intnumberintliteral int 1numberintliteral int 2numberintTopaz compiler 5.21.1 record
primitive stringtextstringliteral string "apple"textstringliteral string "pear"textstringTopaz compiler 5.21.1 record
TPZ5001error
`+` needs matching int, float, string, BigInt, or Decimal operands, found `int` and `string`
Points to cells 1 to 11
In plain wordsThe left is an integer and the right is a string. This plus doesn't take the two together.
In Episode 2, 1 + * 2 could not be put together by the grammar.
This time, 1 + "apple" can be put together by the grammar, but type checking finds a problem.
Code that fits the grammar doesn't always pass type checking.
Not quite.
Even the same symbol can allow different kinds of values, or handle them in different ways, depending on the language.
What we checked today is Topaz's rule.
In one line
A compiler checks what type each part of the code has, and whether it can be used in an operation that fits that type.
If it doesn't fit, the compiler can tell you about the problem before the code runs.
So why doesn't this work?
"3" + 4"3" is a string wrapped in quotes.
4 is a number.
Topaz's + can't mix these two types as they are.
Looking like a number doesn't always make it a number type.
Topaz compiler 5.21.1 record
TPZ5001error
`+` needs matching int, float, string, BigInt, or Decimal operands, found `string` and `int`
Points to cells 1 to 7
In plain wordsThe left is a string and the right is an integer. This plus doesn't take the two together.
Now you can look at the values inside a tree, not just its shape.
Field GuideRevisit these wordsType CheckingError Message
Next story
This time, we have a tree that passed its check.
1 + 2Momo takes the tree and turns it this way and that.
MomoShall we change its shape?Without changing what it means, of course.
Can the same code take on a whole new form?
Next time, let's watch a compiler turn code into a different form.