Skip to content

Conversions

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Conversions

Use Target(value) for an explicit conversion. An annotation is a constraint, with supported Integer-to-Float promotion and nominal wrapping; it is not a request to parse arbitrary text.

let $quantity = Integer("14")
let $ratio = Float("2.5")
echo ($quantity + 1) $ratio Integer(6.0)
15 2.5 6

Integer conversion accepts integral finite Floats, decimal integer text, Boolean, and numeric process statuses. Fractional Floats, out-of-range values, and decimal text such as "2.5" are rejected. Number preserves an existing numeric kind. String removes executable markers from its text result.

Conversion dispatch

For Target(source), lookup tries:

  1. Target::from_<source_type>(source).
  2. Source::to_<target_type>(), with a private receiver copy.
  3. Built-in conversion rules.

An existing hook's failure propagates. It does not silently try another path. Factories take one explicit source argument and have no $self. Source-owned methods use $self and take no arguments.

struct Batch { pub count: Integer }
fn Batch::to_integer(): Integer { $self.count }
fn String::from_batch($batch: Batch): String
{
    "batch=" + String($batch.count)
}
let $batch = Batch(count: 6)
echo Integer($batch) String($batch)
6 batch=6

Names use snake case: ExecResult becomes exec_result, HTTPResponse becomes http_response. Incorrect forms such as to_execresult are rejected. Collection names include their types: array_integer, hash_string_integer. A typedef supplies an overridable factory from its immediate underlying type.

Named conversions and imported extensions participate in the same dispatch. Source-side conversions operate on a copy so changing $self cannot mutate the original value merely because it was converted.

Environment and format conversions

The prelude converts colon-separated text to [String] and arrays to text using Array::from_string / Array::to_string. let type remembers both directions for an environment binding.

Pipeline format hooks are String::from_<format> and <format>::from_string. Those are explicit byte/value boundaries; see Pipeline formats.


Related: Named types · Environment · Pipeline formats

Clone this wiki locally