Documentation contents
Language reference Expressions and operators

Expressions, operators and patterns

The operators and their precedence, text literals, match patterns and the iteration forms. The text and operators and matching guides show them in use.

On this page

Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.

Operators

GroupOperatorsNotes
Arithmetic+, -, *, /, unary -Units follow the operands: + and - need the same quantity, while * and / combine units.
Power^The exponent must be a non-negative whole-number literal, as in x ^ 2. The base is evaluated once. Use pow for a computed, fractional or negative exponent.
Comparison==, !=, <, <=, >, >=Equality is structural: records, unions, optionals and collections are compared element by element.
Logic&&, ||, notOperands are bool.
Text++Joins text. + never joins text.
Fallback??The value inside an optional, or the right-hand side when there is none. An optional right-hand side gives an optional result.
Pipeline|>x |> f is f(x), and chains run left to right. The right-hand side is a function of one argument.

Precedence

From loosest to tightest:

LevelOperators
1??
2||
3&&
4==, !=, <, <=, >, >=
5+, -, ++
6*, /
7Unary -, not
8^
9Calls, field access and indexing

-2 ^ 2 is -4, because ^ binds more tightly than unary minus. A pipeline binds more loosely than arithmetic, so 2 + 3 |> double is double(5). Use parentheses wherever they make the meaning plainer.

Operators and traits

An operator stands for a method of a standard trait, so a type gains the operator by implementing the trait. Built-in numbers keep their built-in operators.

OperatorTrait and method
a + bAdd::add
a - bSub::sub
a * bMul::mul
a / bDiv::div
a == b, a != bEq::eq
a < b and the other orderingsOrd::cmp

Text literals

FormMeaning
"{expr}"Interpolation. The value of expr is converted to text and takes its place. Numbers, bool, char and text convert directly. A type with no text conversion is a compile error.
\n, \r, \tNew line, carriage return, tab.
\\, \"Backslash, double quote.
\{, \}Literal braces.

Any other escape is a compile error. Write other characters, such as accented letters, directly in the UTF-8 source. Text lengths and positions count Unicode scalar values.

Match patterns

match x { P -> e ... } and match x with | P -> e ... are the same expression in two layouts. Arms are tried in order, and an arm may add when followed by a bool condition.

PatternMatchesBinds
_AnythingNothing
nameAnythingname
CaseA union case with no payloadNothing
Case(name)A union case with a payloadThe payload, as name
Case(_)A union case with a payloadNothing
some(name), noneA present or absent optionalThe present value, as name
(P1, P2)A tuple whose parts match P1 and P2What the parts bind

A pattern binds only the names written in it. Text and number literals are not patterns; compare them in a when condition or with if. A match that leaves a union case unhandled draws a warning, and a value that reaches it with no matching arm fails that node.

let some(v) = x else fallback binds the present value, or gives fallback when x is none.

Iteration

FormResult
for x in xs -> eAn array with e for each element.
[for x in xs if c -> e]An array with e for each element where c holds.
fold acc = init with x in xs -> eThe final acc, where e gives the next acc from the current one and x.

Functions and control flow shows each form with rainfall readings.