Ages 15–18 · Grades 9–12 · Computer Science

KindForge

Will this program compile — and what does it print?

A red type error isn’t the compiler being difficult — it’s the compiler catching a bug before your code ever runs. Here you read a short program and predict what the compiler will say: will it compile? If it does, what will it print? If it doesn’t, fix it — without changing what it was meant to do. Some fixes compile but do the wrong thing; the goal is the one that does both.

The code is a teaching subset of the Swift language; the programs are original teaching examples. Runs on your device — nothing leaves it.

Loading the compile lab…

A case · you decide

Yuki's total that came out as text

Yuki, 16 — writing a small Swift program that adds up the points from a tournament spreadsheet

The idea in play: a type is a promise about what a value can be — the compiler checks the promise before the program ever runs.

Yuki reads each score from the spreadsheet as text and writes `let total = score1 + score2`. The scores are "7" and "5". She expects 12. The program prints 75.

Two strings joined with + are glued together, not added. Nothing crashed; the program did exactly what the types said.

Swift would have stopped her if she had tried to add a number to a string. Because both were strings, the code was legal — and wrong.

How should Yuki fix the total?

She now writes `let average = total / 2`. Total is 13. What does the compiler expect her to have noticed?

Read a program →

What next? Pick a door

Chosen from what this app is about. What you open is remembered on this device only.