Examples cookbook
Short, runnable Silicon programs grouped by what they teach. Each page is self-contained — copy the source into src/main.si, sgl run.
Basics — start here.
| Example | Teaches |
|---|---|
| Hello world | Project scaffold, @fn main, @use 'io' |
| Variables | bare (immutable) vs @mut (mutable), inference |
| Functions | @fn, \\ signature lines, parenthesised calls, @export |
| Conditions & loops | @if, @loop, @return, FizzBuzz |
Sum types + @match | @type, variant constructors, arm-expression form |
| Generics | @fn[T], parametric sum types, HM-lite inference |
Error handling with @try | Result[T,E], prefix-keyword unwrap shorthand |
| First-class functions | Function references, call_indirect |
Systems & platforms
| Example | Teaches |
|---|---|
C interop (@extern) | @extern, libc, sgl build --native |
| Rotating cube (raylib FFI) | \\ @extern, native lib linking, scalar-only FFI |
| Arena allocation | with_arena, parent-arena escape |
| Rc smart pointer | Shared ownership, refcount semantics |
Metaprogramming
| Example | Teaches |
|---|---|
| Writing a stratum | Adding a keyword via @stratum |
| Strata as design solvent | Building a small DSL with strata |
How to run any example
sh
sgl init scratch
cd scratch
# paste the example body into src/main.si
sgl runIf an example needs the standard library, add @use 'io'; (or 'num', 'str', …) at the top — sgl init already wires io in for you.
Want more?
The full test suite under src/ is the biggest corpus of real Silicon. Look at any *.test.ts — most embed representative programs as backtick-delimited strings.