Start here / The rules
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
01 Start here / 02
The rules
Hematite's rules on one page: short, strict, and the same for everyone.
These are Hematite's rules in brief, the same ones an agent reads first in hem guide. Each is explained, with examples, on the page its section links to. Together they're most of what makes Hematite code read the same whoever writes it, so they're worth reading once before you start, and again when something surprises you.
Files, lines and names
A Hematite file holds one statement on each line, so you can always see where a statement ends. Names are plain ASCII, and numbers have a few rules of their own. The basics and numbers cover these in full.
- Files use the
.hemextension and must be UTF-8. - One statement per line, with no semicolons. To continue an expression on the next line, end the line with an operator.
- Comments start with
#and run to the end of the line. - Names use only ASCII:
a-z,A-Z,0-9and_, not starting with a digit. Keywords (if,end,and,true, ...) can't be used as names. - Integers are 64-bit; floats need digits on both sides of the dot. Use
_to group digits in long numbers:1_000_000. - Write
a - bora-b, nevera -b, so a minus sign never has to be guessed from its spacing.
Variables and expressions
Every variable is declared before it's used, and says whether it will change. Expressions stay small, and a statement never hides inside one. See variables and calls and expressions.
- Declare every variable before use:
const x = 1if it never changes,var x = 1if it does. Change avarwith plainx = 2. - A variable's type never changes. A
varthat starts as an Int only ever holds Ints; start it as0.0if it will hold Floats. - Declarations and assignments are statements, never part of an expression. One per line:
var x = y = 0is an error. - Keep expressions small. Very long or deeply nested expressions are errors; use variables for intermediate steps.
/always gives a Float:7 / 2is3.5,6 / 2is3.0. For a whole number,7.quotient(2)is3. Dividing by zero is an error for both Ints and Floats.- Compare two values at a time:
1 < x < 3is an error. print(value)prints exactly one value per call.
Calls
Every call has parentheses, even with no arguments: account.total(). Parentheses are what say a method runs; a name without them is a value.
Not this
name.upcase
Write this
name.upcase()
Only the first argument goes without a name, as in add(1, to: 2), unless its parameter is marked named (def resize(named width: Int, height: Int)). Every other argument is named after its parameter, in the order the method declares them. Named arguments shows more.
Conditions, scope and nil
Hematite never guesses what a value means as a condition, or whether it might be missing. There is no "truthiness": the condition of an if, else if or while must be true or false, so if count is an error.
Not this
if count
Write this
if count > 0
- Logic uses words:
and,or,not.&&,||and!don't exist. - Blocks (
if,while) end withendon its own line. The condition is alone on its line too. - To choose between two values, use
x = if cond then a else b. It needselse, can't be nested, and must be a whole value (wrap it in parentheses inside larger expressions). - Variables exist only inside the block (
if,while) that declares them. Declare before the block to use a value after it. Never reuse a name that is already visible.
One nothing: nil
There is one "nothing", nil, and no null or undefined. A variable may only hold nil if its type says so: var found: Int? = nil. A value that might be nil can only be used after checking it, with if found != nil, and there is no way to skip the check. Nil and optional types explains how.
Strings, methods and collections
Text goes in double quotes, methods are defined at the top level of a file, and arrays and hashes hold one type each. Arrays and hashes are values: a copy never changes the original.
Strings
Strings use double quotes only, end on the line they start, and build text with #{}: "Hi, #{name}!". + only joins two strings. More on strings.
Methods
Define methods with def name(param: Type): ReturnType at the top level of a file. Every parameter has a type. Return values with return; a method without a return type returns nil.
A method sees only its parameters, its own variables, statics and other methods. It never sees a var or const from outside. A value that methods or other files need is a static, at the top level of the file. See what a method can see.
Arrays and hashes
- Arrays hold one type:
[1, 2]is a[Int]. They are values: assigning or passing an array copies it, so changes never reach the original. - Reading past the end of an array is an error, never
nil. Indexes start at 0 and can't be negative. - Hashes map one key type to one value type:
{"Ada": 36}is a{String: Int}. Read with.get(key)(might benil: check.has(key)first) or.get(key, fallback: value), write with.set(key, to: value). Neverhash[key].
Arrays and hashes has the rest.
Objects, blocks and types
Classes, traits and enums give your data a shape, and blocks run code for each item. Everything is checked against its type before the program starts.
Classes
Classes declare their fields first, with types: readable const, readable var, writable var, or plain const/var (private). Inside methods, the object's own fields and methods are self.name and self.name(). Create objects with new Name(field: value). The class's own values and methods are static, used as Name.LIMIT and Name.make(...).
Objects are values, like arrays and hashes. A method that changes self can only be called on a var. See classes and records.
Traits and enums
- Share fields and methods between classes with a trait:
trait Name, thenwith Nameat the top of each class that uses it. There is no inheritance. - A fixed set of named values is an enum:
enum Status, then one value per line. Always write values in full:Status.draft. Choose by value withwhen statusandis Status.draftlines, which must list every value; there is noelse.
Both are on traits and enums.
Blocks
A block looks like items.each do (item) ... end. Parameters go in parentheses, with no types. Inside a block, return ends just the block. map's block declares what it returns: do (n): Int. More on blocks.
Numbers and types
- Round explicitly:
x.floor(),x.ceil()andx.round()give an Int;x.round(2)keeps 2 decimals and gives a Float. Convert withFloat(x),Int(x)andString(x). - Only the methods listed under built-in methods exist on built-in values.
- Types are checked before running. A program with a type error doesn't start, even if the mistake is in code that would never run.
Files, failures and the outside world
A program grows into several files, and talks to files, input, the network and JSON. What can go wrong out there, like a missing file or text that isn't JSON, is a failure, and the code handles it at the call.
- Split a program over files: mark what other files may use with
export, andimport "path"it at the top of the file that uses it. A file's other top-level code runs only when it's the file being run. See imports and exports. - A method that can fail marks its return type with
!(: Int!, or: !when it returns nothing) and fails withreturn failure("message"). Each call to it handles the failure:try call()passes it on;call() rescue error, a block andendhandles it there, the block giving the value withreturn.Int(text)andFloat(text)can fail. See failures. - Files and folders are reached through
FileandFolder:File.read(path, as: String),File.write(path, content: s),Folder.list(path), and so on. Paths are relative to wherehemruns; most of these can fail. Program.argumentsare the program's arguments,Program.env(name)an environment variable (String?), andInput.line()the next line of input (String?, nil at the end);Input.line("Name: ")asks first. See files, input and output.- A record is plain data:
record Point, thenx: Intfields, which are always readable and never change. JSON works with records:try JSON.decode(text, as: [User]),JSON.encode(user). See JSON.
Forms and pages
Hematite reads forms and writes pages. Serving and HTTP requests are the http package's, and web applications are built with Kiln, a package too.
- A form's text becomes a record with
try Form.decode(text, as: SignUp). See forms. - Pages are Ochre templates (
.och), written as Hematite: an element is a line starting with its name (p title), its attributes a line each under it (class: "card"), then what's in it, andend. Imported with a name and shown withpage.render(...), which givesHtml. Text is always escaped.