The language / The basics
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
02 The language / 01
The basics
Comments, lines, names, statements and operators: how Hematite code is put together.
Before values and variables come a handful of rules that decide what a Hematite file looks like at all: where a comment starts, where a statement ends, which names you may choose and how operators group. Each rule is small and strict. Hematite usually offers one way to write each thing, so code reads the same whoever wrote it, and the person reviewing it never has to guess.
Comments
A comment starts with # and runs to the end of the line. It can have a line to itself, or follow code on the same line:
# A full-line comment
const x = 1 # a comment after codeInside a comment anything goes: any characters, including Unicode and emoji. The strict rules for names further down don't reach into comments.
Lines and whitespace
Hematite reads a file line by line, and a line break ends a statement. There is no semicolon to write or to forget: where the line ends, the statement ends. (A long expression can still carry on to the next line; long expressions shows how.)
Spaces and tabs separate the pieces of a line, such as names, numbers and operators, and are otherwise ignored. There is one exception, a space before a minus sign, which is under operators below. Indent with 2 spaces; hem fmt lays every file out that way for you (see formatting).
Windows line endings (\r\n) are fine, so a file reads the same whichever system saved it.
Names
Names, also called identifiers, are what you give to variables and to methods. A name may use only these characters:
- the letters
a-zandA-Z, - the digits
0-9, though not as the first character, - and the underscore,
_.
Write variables in snake_case: lower-case words joined by underscores. All of these are fine:
const total = 1
const max_retries = 3
const _unused = 0
const value2 = 2Letters outside a-z and A-Z, such as å, and symbols such as emoji are errors, and each error says which character is the problem:
blåbær = 1 # error: 'å' is not allowed in names; use only a-z, A-Z, 0-9 and '_'
🦀 = 1 # error: unexpected character '🦀'Keywords
These words have a meaning in Hematite, so they can't be used as names:
truefalsenilconstvarifthenelseendwhileandornotdefreturnclassselfnewreadablewritableprivatedobreakcontinueenumwhenistraitwithimportexportasstatictryrescuefailurenamedrecordforeignextern
Using one as a name is an error. What the error says depends on the word, since Hematite reads it as the keyword first:
end = 1 # error: 'end' without an open block ('if', 'while', 'when', 'def', 'class', 'enum' or 'do')
if = 1 # error: 'if' is a keyword and can't be used as a nameA name that only contains a keyword is fine: end_date and nil_count are ordinary names. Some words you might expect to be reserved, such as module or unless, are plain names in Hematite today. One keyword is also a parameter's name: a method may take as, as calls like JSON.decode(text, as: [User]) write it (def sign_in(page: Page, as: Int), called sign_in(page, as: id)).
Statements
A program is a list of statements, one per line, run from the top. Blank lines and comment lines between statements are ignored, so you can use them to group lines that belong together:
const width = 3
const height = 4
const area = width * heightBecause the end of the line is the end of the statement, two statements on one line is an error:
const x = 1 y = 2 # error: expected end of line, found 'y'Declaring a variable and changing one are statements too, which is why they can't be tucked inside an expression; variables has the details.
Operators
Operators combine values: arithmetic with + and *, comparisons with == and <, logic with and and or. When an expression has several, precedence decides which go together first. Here are all of them, from the tightest binding to the loosest:
| Precedence | Operators | Groups |
|---|---|---|
| 1 (tightest) | -x not x | right to left: --x is -(-x) |
| 2 | * / % | left to right: 8 / 4 / 2 is (8 / 4) / 2 |
| 3 | + - | left to right: 1 - 2 - 3 is (1 - 2) - 3 |
| 4 | == != < <= > >= | never chained: a < b < c is an error |
| 5 | and | left to right |
| 6 (loosest) | or | left to right: a or b and c is a or (b and c) |
So 1 + 2 * 3 multiplies first, and - and not bind tightest of all, to the value right after them. Comparisons are the one level that never groups: you compare two values at a time, and comparisons shows how to write a range check instead. and, or and not are covered in logic.
To group differently, use parentheses: (1 + 2) * 3. Use them too whenever a reader might have to stop and think about precedence, even where they change nothing.
Assignment (=) is not in the table, because it is not an operator; it is a statement of its own. And there is no unary plus: +1 is an error. Write 1.
Spaces around a minus sign
Spacing matters in one case, the minus sign. x - 1 and x-1 both subtract. x -1, with a space before the - but not after, is an error, because it could mean either "x minus 1" or "call x with -1", and Hematite won't guess:
const y = x -1 # error: ambiguous '-' with a space before but not after; write 'a - b' to subtract, or 'name(-b)' to pass a negative numberA minus straight after another operator or = is a plain negative, and is fine: x * -1, y = -x.