Episode 2 Twig's Assembly Time

The Life of Code 03

Tami's Suspicious Sum

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?

Today's odd questionWill Topaz add this up?1 + "apple"

On the left there is a number, and on the right there are letters wrapped in quotes.

How will the Topaz compiler handle this expression?

So that's your guess.

Let's follow along and see what Tami looks at.

Comic

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.

Scene 2 Tami's magnifying glass

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.

Scene 3 The first tag

TamiThe left one is checked.

LineyWhat about the right?

TamiThat's next.

Scene 4 The plus sign stops short

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.

Scene 5 The card Tami left

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

Tami's Magnifying Glass

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.

Pick an expression.

Hold the magnifying glass over the left and the right.

A tag will show up on each one, saying what it is.

Look at the left value

1numberint

Look at the right value

2numberint

Now take a look at the +.

Can this plus take both kinds together?

Look at the plus sign

This pair is fine.

In this expression, the result of the plus is a number type too.

Resultnumberint

See the real type record
Real type record

Topaz compiler 5.21.1 record

This record comes from the compiler of Topaz, the language this site runs its examples in.
  • Whole expression cells 1 to 5primitive intnumberint
  • Value cell 1literal int 1numberint
  • Value cell 5literal int 2numberint

Hold the magnifying glass over the left and the right.

A tag will show up on each one, saying what it is.

Look at the left value

"apple"textstring

Look at the right value

"pear"textstring

Now take a look at the +.

Can this plus take both kinds together?

Look at the plus sign

This one is fine too.

In Topaz, you can join two strings together with +.

The type of the result is a string too.

Resulttextstring

See the real type record
Real type record

Topaz compiler 5.21.1 record

This record comes from the compiler of Topaz, the language this site runs its examples in.
  • Whole expression cells 1 to 16primitive stringtextstring
  • Value cells 1 to 7literal string "apple"textstring
  • Value cells 11 to 16literal string "pear"textstring

Hold the magnifying glass over the left and the right.

A tag will show up on each one, saying what it is.

Look at the left value

1numberint

Look at the right value

"apple"textstring

Now take a look at the +.

Can this plus take both kinds together?

Look at the plus sign

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.

See the real compiler error
Real error

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.

Look at the left value

"1"textstring

Look at the right value

2numberint

Now take a look at the +.

Can this plus take both kinds together?

Look at the plus sign

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.

See the real compiler error
Real error

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"textstringtextstringAccepted
  • 1 + "apple"numberinttextstringLook again
  • "1" + 2textstringnumberintLook again
Compare the tags
1 + 2numberintnumberintResultnumberint
"apple" + "pear"textstringtextstringResulttextstring

Both 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"textstring

Just a pair of quotes can change the type.

The real inside story

Values come in kinds too

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.

numberint

The type for integers, which are whole numbers.

Values like 1, 2 and 4 belong here.

textstring

The type for strings, which are pieces of text.

Letters wrapped in quotes, like "apple", "pear" and "1", belong here.

Pairs the plus can take

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.

How does the real compiler write it down?

Real type record

Topaz compiler 5.21.1 record

This record comes from the compiler of Topaz, the language this site runs its examples in.
  • Whole expression cells 1 to 5primitive intnumberint
  • Value cell 1literal int 1numberint
  • Value cell 5literal int 2numberint
Real type record

Topaz compiler 5.21.1 record

  • Whole expression cells 1 to 16primitive stringtextstring
  • Value cells 1 to 7literal string "apple"textstring
  • Value cells 11 to 16literal string "pear"textstring
Real error

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.

How is this different from the last error?

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.

Is every language the same?

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.

See the records of four languages in the Language Tour

In one line

What we found out today

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
Find out why

"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.

Real error

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

Episode 4Momo's Transformation Factory

This time, we have a tree that passed its check.

1 + 2

Momo 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.