If Topaz is installed, open an empty working folder in a terminal and a text editor, and save the files below there. If it is not, install Topaz first and come back here, or run the same code in the Playground without installing anything.
Write and run a study plan
Save this code as first-program.tpz in your working folder.
import std.io
let topic = "Topaz basics"
let minutes: int = 25
io.writeLine("{topic}: {minutes} minutes")In that folder, check the file and run it.
topaz check first-program.tpz
topaz run first-program.tpzcheck reports the type and name checks without running the file.
first-program.tpz: types-ok (2 modules)
first-program.tpz: resolve-ok (2 modules)run checks the file before printing this result.
Topaz basics: 25 minutesThe first let names a string, so the checker infers its type. The second let declares minutes as an int, an integer. io.writeLine inserts the values inside {...} into the output.
Read and fix an error
Copy first-program.tpz to a second file named first-program-error.tpz. In the copy, change the fourth line to this deliberate type error.
let minutes: int = "25"Check the second file.
topaz check first-program-error.tpzThe checker exits with an error. It shows the expected type, the actual type, and the location to fix.
error[TPZ5001]: expected `int`, found `string`
--> first-program-error.tpz:4:20
|
4 | let minutes: int = "25"
| ^^^^
first-program-error.tpz: 1 type diagnosticIn first-program-error.tpz, change the highlighted "25" to 25. The quotes made it a string. Now topaz check first-program-error.tpz succeeds, and topaz run first-program-error.tpz prints Topaz basics: 25 minutes.
Every diagnostic has a TPZ#### code. To read more about one, run topaz explain TPZ5001 or see CLI Commands.
Choose check or run
Use topaz check for type and name errors while editing, without starting the program. Use topaz run to perform the same checks and then see what the program does. Test the output to check your program logic.
Try it
Change topic to "Functions" and minutes to 30. What should the program print?
Answer
It prints Functions: 30 minutes. The values still have types string and int, so the checker accepts them.
Continue when the program works
You are ready when the corrected file passes topaz check and prints the expected line with topaz run.
Continue to the Learning Path to choose where the lessons begin. Keep Syntax at a Glance nearby to look up syntax.