// LANGUAGE PREVIEW — compiles and runs in this browser playground.
// Runs on the pinned Rust/Wasm compiler; Java/LLVM parity is not claimed.
// Result/Option generic ADTs.
// Ok(value) and Err(error) are explicit Result values; Some/None are Option.
// Err and None are values, not throws. Neither is caught by `trap` or implicitly coerced.
// See the trap preview for `trap checked_id(5)` and its outer Option layer.

fnc validate_port(int port): Result<int, string> {
  if port <= 0 then
    return Err("port must be positive");
  fi
  return Ok(port);
}

fnc optional_port(bool present): Option<int> {
  if !present then
    return None;
  fi
  return Some(8080);
}

pub fnc main(): int {
  match validate_port(8080) over
    on Ok(port) -> { stdio.println(port); }
    on Err(message) -> { stdio.println(message); }
  end
  // Match arms produce values; only the explicit return exits main.
  return match optional_port(false) over
    on Some(port) -> port,
    on None -> 0
  end;
}
