1. Hello

Goal

Show a sentence on the stage.

New in this chapter

Steps

Make a folder hello and put a file hello.synod in it. Every step is that one file.

Synod has no setup to learn first. A program is a folder, and the folder's start is a file whose first line says it is the program:

Step 1

# package main
use std/lesson

define main
  lesson.Note "Hello, Synod"

Line 1 is the header: # package main says "this file is the folder's program". Line 2 is a use: it brings in std/lesson, a small package from the standard library that talks to the viewer. Then define main says what happens when the program starts. Its body is the indented line below it, a block, and it is a sentence: lesson.Note followed by the text "Hello, Synod". Run it with synod run hello.

A line that starts with # is a comment. It belongs to the line below it, so a comment above define main describes the program:

Step 2

# package main
use std/lesson

# Say hello.
define main
  lesson.Note "Hello, Synod"

A block holds as many sentences as you like, one per line, all indented the same:

Step 3

# package main
use std/lesson

# Say hello.
define main
  lesson.Note "Hello, Synod"
  lesson.Note "Every line reads as a sentence."

What to watch

The stage is empty, and a caption appears: first the greeting, then the second sentence. Nothing else exists yet, so there is nothing to draw but the words. Each lesson.Note is one caption, shown in the order you wrote them.

Why it looks like this

Indentation is two spaces per level, and it is the only way to say that lines belong together. A tab is not a different kind of space; it is an error you can see at once:

hello/hello.synod:6:1: error[indent]: tabs are not allowed
  |
6 |     lesson.Note "Hello, Synod"
  | ^^^^
  |
  = help: indent with 2 spaces per level
Error indent

The diagnostic is indent. For every other detail of layout (blank lines, spacing, the order of use lines) you never have to remember a rule: synod fmt hello rewrites the file into the one canonical form, and synod fmt --check hello tells you if a file differs. Synod has one way to write each thing, so code looks alike everywhere (spec/10).

Exercises

  1. Add a third lesson.Note line. How many captions do you see?
Expected

Three, in the order written.

  1. Delete the use std/lesson line and run synod check hello. What does it say?
Expected

lesson.Note is an unknown phrase: a package must be imported before you use its phrases.

  1. Indent one lesson.Note line by three spaces and run synod fmt --check hello, then synod check hello.
Expected

The indentation is not a multiple of two spaces, so the file is rejected. Indent by two spaces per level and it passes.