---
title: Types and expressions
description: Follow typed payloads through guards, narrowing and pure
  expressions without confusing text, bytes or decoded JSON.
url: https://docs.rastok.com/guide/02-types-and-expressions
source_revision: v0.2.0-alpha
---

# Types and expressions

After this chapter you can read payload types and guarded transitions,
distinguish business rejections from evaluation faults, and explain why
decoded JSON still needs shape validation.

## Give the payload a shape

A port carries an envelope: `data` holds its payload; `meta` holds provenance
and application metadata. The payload has one resolved logical type,
inferred before execution if the port declaration omits it.

`type Name = ...` names a set of allowed values. `output out: Webhook`
requires one of those values. See the [type reference](/reference/types.md).

The webhook example names the shape it needs. This declaration is from
`examples/webhook-to-partner.rkg`, lines 3–7:

```rkg
type Webhook = struct {
  event_id: string
  email: string @personal(identifier = true)
  amount: int
}
```

A `Webhook` requires two strings and a signed 32-bit integer. The
annotation on `email` adds personal-data policy without changing its type.

An assignment widens if every value fits:

| From | Example destinations |
|---|---|
| `int` | `long`, `double`, `decimal` |
| `uint` | `long`, `ulong`, `double`, `decimal` |
| `long`, `ulong` | `decimal` |
| `float` | `double` |

| Peers | Common type |
|---|---|
| `u + 1`, unsigned `u` | `uint` |
| `b ? i : l`, `int` and `long` arms | `long` |

Use `data.d as? int` for a particular `double`: 7.0 gives 7; 7.5 gives
null. See the [numeric reference](/reference/types.md#numeric-types).

Named type declarations are structural aliases: their checked shape matters,
not their name. Tagged-union case names remain part of the value. For
`enum { Pending, Cancelled }`, write `out = Cancelled` where that enum is
expected, or compare `data.status == Cancelled`. A conflicting variable name
is refused.

Types such as `FileExit` and `ProcessExit` come from the standard library;
use them without copying their declarations or importing them. The
[standard library reference](/reference/stdlib.md#types) lists
these names. Modules and explicit imports remain a later design discussion.

## Test before projecting

With `codec = json`, the webhook listener supplies a `variant`: a decoded
value whose application shape is not yet known. An Operator cannot simply
read arbitrary members of it. It first tests and narrows the value.

The validation Operator, from the same file, lines 30–41, separates shape
validation from the business rule:

```rkg
operator validate {
  input in: variant
  output out: Webhook
  output failed: Failure
  on in match {
    data is Webhook => match {
      data.amount > 0 && size(data.event_id) > 0 => out = data as Webhook
      _ => failed = Rejected { reason = "positive amount and event id required" }
    }
    _ => failed = Rejected { reason = "expected event_id, email and integer amount" }
  }
}
```

`is` asks whether the value fits a type or union case. On the successful
branch, member access such as `data.amount` is checked with the narrower
shape. `as` requests a conversion that succeeds only for a compatible value.
The final `_` branch makes the alternative visible.

These rejections emit `Rejected` on `failed`. A negative amount is a
business outcome; malformed JSON is a Connector outcome.

When `dossier: Open?` may be absent, use `dossier.?title.orValue("No dossier is open")`.
A missing array element or map key gives null: `names[?i]`, `counts.?pending`.
If the payload might not fit a type, `data as? Webhook` returns the value
or null. A plain cast needs a proof, as in the successful branch above.

A string can be tested directly: `subject.startsWith("RE:")` checks a
prefix, `subject.contains("urgent")` a substring, and
`filename.endsWith(".csv")` a suffix. All three return a boolean.

## Keep bytes and text distinct

File reads produce `array<byte>`. Its inspection Operator, from
`examples/file-pipeline.rkg`, lines 16–25, accepts only nonempty bytes:

```rkg
operator inspect {
  input in: array<byte>
  output accepted: array<byte>
  output bytes_read: int
  output empty: bool
  on in match {
    size(data) > 0 => { bytes_read = size(data); accepted = data }
    _ => empty = true
  }
}
```

The successful branch emits two values, in source order: a size and the
unchanged bytes. The alternative emits a boolean and never sends bytes to
the writer. `size` on bytes counts bytes. The example's UTF-8 scenario
therefore counts three bytes for `é` followed by a newline, not two characters.

Ports can carry timestamps, durations or null alone. `string(duration("5s"))`
returns `"5s"`; `"id-" + string(42)` returns `"id-42"`. A typed
[HttpClient response](/reference/connectors/http-client.md) decodes UTC timestamps.

## Understand the expression boundary

Expressions are pure, deterministic and evaluated under bounds. They do
not read a file, contact a server or obtain randomness. Guards such as
Echo's `on in where data != ""` decide whether a handler runs; `match`
selects an explicit branch inside the transition.

Malformed dynamic text passed to `duration`, `timestamp` or `decimal`
yields null; a malformed constant is refused at check.
Division by zero still faults without committing a partial transaction
or inventing an `err` output. The Lab runs the same expressions.

The authorities are [`docs/spec/types.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/types.md), [`docs/spec/expressions.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md) and
[`docs/spec/stdlib.md`](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/stdlib.md). This chapter covers the executable examples, not
every proposed frontend or codec. See
[what is available today](/index.md#what-is-available-today) for the alpha limits.

## Summary

- A resolved type is different from an external encoding.
- `is` tests a shape; `as` performs a checked conversion.
- Expected rejection is typed application data; evaluation faults are not
  implicit output ports.
- Lab and App execute the same Operator expressions.

## Next

Read [Connectors and outcomes](/guide/03-connectors-and-outcomes.md), then try
[File pipeline](/examples/02-file-pipeline.md). Its empty and UTF-8
scenarios make the distinction between payload type and representation concrete.
