Documentation contents
Language guide

Text and operators

Build station labels and reports from text, then use the operators: arithmetic, comparison and logic, pipelines, fallbacks, and operators on types of your own.

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.

Joining text

Text is joined with ++. The + operator is kept for arithmetic, so a sum and a join never look alike.

fn heading(station: text) -> text =
    text.to_upper(station) ++ " STATION"

node Heading = heading("Ridge")
node HeadingLength = text.len(Heading)

Heading is "RIDGE STATION". text.len counts characters, so its length is 13.

Putting values into text

Write an expression in braces inside a text literal and its value takes that place. Numbers, booleans and text go in directly, with no conversion call.

fn label(station: text, readings: i64) -> text =
    "{station}: {readings} readings"

node Label = label("Harbor", 3)

Label is "Harbor: 3 readings". Any text literal can hold braces. Myceta has no separate prefix for literals that interpolate.

Special characters

A backslash starts an escape: \n for a new line, \t for a tab, \" for a quote, \\ for a backslash, and \{ and \} for braces that should appear as they are.

fn row(cells: array<text>) -> text =
    text.join(cells, "\t")

fn braces(name: text) -> text =
    "\{" ++ name ++ "\}"

braces("station") gives {station} as plain text. Any other escape is an error. Write other characters, such as accented letters, directly in the source file.

Working with text

The text module has the everyday operations: len, to_upper, to_lower, replace, split and join.

fn normalized(name: text) -> text =
    text.replace(text.to_lower(name), " ", "-")

node Slug = normalized("North Ridge")

Slug is "north-ridge". Lengths and positions in text count characters, not bytes.

Arithmetic, comparison and logic

The arithmetic operators are +, -, * and /, and the comparisons are ==, !=, <, <=, > and >=. Units follow the values through all of them, as units of measure explains. Logic uses &&, || and not.

fn stormy(rain: f64<mm>, gusts: bool) -> bool =
    rain >= 30.0<mm> && gusts || rain >= 60.0<mm>

fn calm(rain: f64<mm>, gusts: bool) -> bool =
    not stormy(rain, gusts)

&& binds more tightly than ||, so a day is stormy when it has heavy rain with gusts, or very heavy rain alone. Add parentheses whenever they make the reading clearer.

For a small whole-number power, write ^.

fn square(x: f64<>) -> f64<> = x ^ 2

The exponent must be a whole number written in the source. Use pow when it is computed, fractional or negative.

Operators for your own types

Each operator stands for a method of a standard trait: + for Add, * for Mul, comparisons for Ord and equality for Eq. Implement the trait, and the operator works on your type.

type Tally = { readings: i64; rainfall: f64<mm> }

impl Add for Tally {
    type Output = Tally
    fn add(self: Self, rhs: Self) -> Self::Output =
        Tally { readings = self.readings + rhs.readings; rainfall = self.rainfall + rhs.rainfall }
}

node Morning = Tally { readings = 2; rainfall = 12.5<mm> }
node Evening = Tally { readings = 3; rainfall = 22.0<mm> }
node Day = Morning + Evening

Day holds five readings and 34.5 mm of rain. Traits, generics and modules covers traits in full.

Pipelines

|> passes the value on its left to the function on its right, so a chain of steps reads in the order it runs. Here a sixth reading is added to the day's tally and the total read off.

fn addReading(t: Tally, rain: f64<mm>) -> Tally =
    t with { .readings = t.readings + 1, .rainfall = t.rainfall + rain }

fn total(t: Tally) -> f64<mm> = t.rainfall

node Updated = addReading(Day, 4.0<mm>) |> total

Each step takes one argument. Call a function that needs more than one argument in the usual way, as addReading is called here, and pipe its result on.

A fallback for a missing value

?? gives the value inside an optional, or the right-hand side when there is none. It binds more loosely than every other operator.

fn chosen(preferred: optional<text>) -> text =
    preferred ?? "Harbor"

node Preferred = chosen(some("Ridge"))
node Fallback = chosen(none)

Collections and missing values covers optional in full.

Run the example

The complete module is station-text.myc, run by the test manifest. The next page covers collections and missing values.