1. Hello
Goal
Show a sentence on the stage.
New in this chapter
# package mainform header spec/06 §6.1# Say hello.form comment spec/01 §1.3- indented lines under
define mainform block spec/01 §1.2 "Hello, Synod"form value-text spec/01 §1.6use std/lessonform use spec/06 §6.3define mainform main spec/06 §6.5"Hello"type text spec/04 §4.1define maincontext main spec/05 §5.4lesson.Notepackage std/lesson std/README.md § std/lesson
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
indentThe 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
- Add a third
lesson.Noteline. How many captions do you see?
Expected
Three, in the order written.
- Delete the
use std/lessonline and runsynod check hello. What does it say?
Expected
lesson.Note is an unknown phrase: a package must be imported before you use its phrases.
- Indent one
lesson.Noteline by three spaces and runsynod fmt --check hello, thensynod 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.