StdErr

The StdErr module provides functions for writing to standard error output. This is typically used for error messages, warnings, and diagnostic information that should be separated from normal program output.

Standard Error vs Standard Output

Standard error (stderr) is separate from standard output (stdout). This allows users to redirect normal output to files while still seeing errors on the console, or to capture errors separately for logging and monitoring.

Writing to StdErr

StdErr.write
a -> ()
Writes any value to standard error without adding a newline.
StdErr.write "Error: "
StdErr.write "File not found"
# stderr: Error: File not found
StdErr.write-line
a -> ()
Writes any value to standard error followed by a newline.
StdErr.write-line "Error: Configuration file missing"
StdErr.write-line "Warning: Using default settings"
# Each message appears on its own line

Usage Examples

Error Logging

Use stderr for error messages while keeping normal output clean:

process-file = fn(filename) =>
  match File.read filename
    | Ok content ->
        # Normal output goes to stdout
        println "Processing ${filename}"
        content
    | Err error ->
        # Errors go to stderr
        StdErr.write-line "Error reading ${filename}: ${error}"
        ""

Diagnostic Output

Log diagnostic information without mixing it with program output:

debug = fn(message) =>
  match Env.get "DEBUG"
    | Some _ -> StdErr.write-line "[DEBUG] ${message}"
    | None -> no-op

process-data = fn(data) =>
  debug "Starting data processing"
  result = data |> transform |> validate
  debug "Processing complete: ${length result} items"
  result

Progress Reporting

Show progress indicators on stderr:

process-batch = fn(items) =>
  items |> each (fn(item) =>
    StdErr.write "."
    process-item item
  )
  StdErr.write-line " done"

# Shows dots as each item completes: ........ done

Warning Messages

Issue warnings without disrupting normal output flow:

warn = fn(message) =>
  StdErr.write-line "Warning: ${message}"

load-config = fn(path) =>
  match File.exists? path
    | true -> File.read path
    | false ->
        warn "Config file not found at ${path}"
        warn "Using default configuration"
        default-config

Structured Error Reporting

Output structured error information for logging systems:

log-error = fn(level, code, message) =>
  timestamp = Time.now
  error-record = {
    timestamp: timestamp,
    level: level,
    code: code,
    message: message
  }
  StdErr.write-line error-record

# Usage
log-error "ERROR" 500 "Database connection failed"
log-error "WARN" 301 "Deprecated API usage detected"