📚 Mica Examples

Runnable programs showcasing language features

Featured Examples

All examples are runnable with cargo run --bin mica -- --run examples/<filename>.mica

Comprehensive Deployment

Advanced 166 lines

An end-to-end deployment pipeline demonstrating:

  • Structured concurrency with spawn/await
  • Nested using blocks with RAII semantics
  • Pattern matching with guards
  • Capability-aware error propagation
  • Retry logic with exponential backoff

Concurrency Pipeline

Intermediate 56 lines

A fan-out/fan-in pipeline showing:

  • Task spawning and awaiting
  • Pattern matching on results
  • Capability-checked IO operations
  • Structured error handling

Generic Tree Algorithms

Intermediate 45 lines

Recursive ADTs with generic functions:

  • Generic type parameters
  • Recursive algebraic data types
  • Higher-order functions (map, fold)
  • Type inference

Effects & Resource Pool

Intermediate ~1KB

Higher-order effect management:

  • Resource pooling with RAII
  • Higher-order capability helpers
  • Cross-capability tasks
  • Structured error propagation

Algebraic Data Types

Beginner ~240 bytes

Core ADT features:

  • Type definitions (Option, Result)
  • Generic type parameters
  • Pattern matching
  • Exhaustiveness checking

Methods & Implementations

Beginner ~226 bytes

Object-oriented features:

  • Impl blocks
  • Method receivers
  • Method call syntax
  • Lowering to functions

Match with Guards

Beginner ~359 bytes

Advanced pattern matching:

  • Pattern guards (if conditions)
  • Exhaustiveness with guards
  • Nested patterns
  • Wildcard matching

Lists & Loops

Beginner ~167 bytes

Collection manipulation:

  • List literals
  • For loops
  • Mutable variables
  • Iteration patterns

Loop Control Flow

Beginner ~475 bytes

Control flow constructs:

  • While loops
  • Break statements
  • Continue statements
  • Conditional logic

Results & Try Operator

Intermediate ~586 bytes

Error handling patterns:

  • Result type usage
  • Try operator (?)
  • Error propagation
  • Recoverable errors

Effects & Using Blocks

Intermediate ~268 bytes

Capability system basics:

  • Effect annotations
  • Using blocks (RAII)
  • Resource management
  • Explicit capabilities

Native Binary Entry

Beginner ~150 bytes

Minimal native compilation:

  • Main function entry point
  • Native --build workflow
  • Binary generation
  • End-to-end compilation

Running Examples

# View AST cargo run --bin mica -- --ast examples/adt.mica # Check for errors cargo run --bin mica -- --check examples/concurrency_pipeline.mica # Lower to HIR cargo run --bin mica -- --lower examples/methods.mica # View typed IR cargo run --bin mica -- --ir examples/generics_tree_algorithms.mica # Build native binary cargo run --bin mica -- --build examples/native_entry.mica # Compile and run cargo run --bin mica -- --run examples/comprehensive_deployment.mica

For more details, see the Language Tour and CLI Snippets.