Documentation contents
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.
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
| Group | Operators | Notes |
|---|---|---|
| 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 | &&, ||, not | Operands 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:
| Level | Operators |
|---|---|
| 1 | ?? |
| 2 | || |
| 3 | && |
| 4 | ==, !=, <, <=, >, >= |
| 5 | +, -, ++ |
| 6 | *, / |
| 7 | Unary -, not |
| 8 | ^ |
| 9 | Calls, 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.
| Operator | Trait and method |
|---|---|
a + b | Add::add |
a - b | Sub::sub |
a * b | Mul::mul |
a / b | Div::div |
a == b, a != b | Eq::eq |
a < b and the other orderings | Ord::cmp |
Text literals
| Form | Meaning |
|---|---|
"{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, \t | New 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.
| Pattern | Matches | Binds |
|---|---|---|
_ | Anything | Nothing |
name | Anything | name |
Case | A union case with no payload | Nothing |
Case(name) | A union case with a payload | The payload, as name |
Case(_) | A union case with a payload | Nothing |
some(name), none | A present or absent optional | The present value, as name |
(P1, P2) | A tuple whose parts match P1 and P2 | What 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
| Form | Result |
|---|---|
for x in xs -> e | An 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 -> e | The final acc, where e gives the next acc from the current one and x. |
Functions and control flow shows each form with rainfall readings.