Skip to content

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.

ExampleTeaches
Hello worldProject scaffold, @fn main, @export
Sum types + @match@type, variant constructors, arm-expression form
Generics@fn[T], parametric sum types, HM-lite inference
Error handling with @tryResult[T,E], prefix-keyword unwrap shorthand
Arena allocationwith_arena, parent-arena escape
Rc smart pointerShared ownership, refcount semantics
Writing a stratumAdding a keyword via @stratum
Strata as design solventBuilding a small DSL with strata
QBE native compilesgl build --release, freestanding binary
First-class functionsFunction references, call_indirect

How to run any example

sh
sgl init scratch
cd scratch
# paste the example body into src/main.si
sgl run

If an example needs the standard library, add @use 'stdlib/path.si'; at the top — sgl init already wires io.si in for you.

Want more?

These ten cover the surface; 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.