Expressions & Operators

Expressions in Compact produce values and have static types. Every expression either evaluates to a value or raises an exception.

Expression Categories

Compact supports several categories of expressions:

CategoryExamples
Literals42, true, "hello"
Arithmetica + b, x - y, n * m
Comparisona == b, x < y, n >= m
Logicala && b, x || y
Conditionalcond ? a : b
Type Castvalue as Uint<64>

Evaluation Rules

Expressions evaluate their subexpressions in order. If any subexpression raises an exception, the entire expression stops and raises that exception.

// Type errors are caught at compile time
const x: Uint<0..255> = 300;  // Error: 300 exceeds range 0..255

// Runtime exceptions propagate up
const result = checked_operation();  // May fail at runtime

In This Chapter