Skip to content

Conversion Operator (::)

The conversion operator :: allows you to convert values from one type to another. It takes a value on the left side and a type on the right side, returning the converted value.

Syntax

value :: targetType

Supported Conversions

Text to Number

Convert text strings to numbers. Returns nothing if the text cannot be parsed as a valid number.

123.45
from '123.45' :: number
from 'not a number' :: number
// Result: nothing

Text to Instant

Convert ISO 8601 formatted text to instant values. Returns nothing if the text is not a valid date format.

from '2023-12-25T10:30:00Z' :: instant
// Result: 2023-12-25T10:30:00Z
from 'invalid date' :: instant
// Result: nothing

Number to Duration

Convert numbers to duration values of various units.

from 24 :: hours
// Result: 24 hours
from 90 :: minutes
// Result: 90 minutes
from 3600 :: seconds
// Result: 3600 seconds
from 7 :: days
// Result: 7 days
from 2 :: weeks
// Result: 2 weeks
from 6 :: months
// Result: 6 months
from 3 :: years
// Result: 3 years

Duration to Duration

Convert between different duration units. Note that these conversions are approximate for units like months and years due to their variable lengths.

from 2 hours :: minutes
// Result: 120 minutes
from 1 days :: hours
// Result: 24 hours
from 1 months :: days
// Result: 30 days (approximate)

Any Value to Text

Convert any value to its text representation.

from 42 :: text
// Result: "42"
from true :: text
// Result: "true"
from nothing :: text
// Result: ""

Chaining Conversions

You can chain multiple conversions together:

from '24' :: number :: hours
// Result: 24 hours

Constant Folding

When converting constant values at compile time, the conversion is optimized:

from '100' :: number
// Optimized to: 100 (at compile time)

Type Safety

The conversion operator performs type checking at compile time. For conversions that might fail (like text to number), the result type is a union type that includes nothing:

  • text :: number returns number|nothing
  • text :: instant returns instant|nothing
  • value :: text returns text
  • number :: duration returns the specific duration type

Examples

// Basic conversions
from '42' :: number + 8
// Result: 50
// Duration arithmetic
from 2 :: hours + 30 :: minutes
// Result: 2.5 hours
// Text formatting
from (42 + 8) :: text
// Result: "50"
// Error handling with union types
let result = '123abc' :: number
// result is of type number|nothing

Notes

  • Text to number conversion supports scientific notation (e.g., “1.23e4”)
  • Text to instant conversion expects ISO 8601 format
  • Duration conversions preserve the original numeric value with the new unit
  • Any value can be converted to text using its string representation