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.

Basics — start here.

ExampleTeaches
Hello worldProject scaffold, @fn main, @use 'io'
Variablesbare (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 @tryResult[T,E], prefix-keyword unwrap shorthand
First-class functionsFunction references, call_indirect

Systems & platforms

ExampleTeaches
C interop (@extern)@extern, libc, sgl build --native
Rotating cube (raylib FFI)\\ @extern, native lib linking, scalar-only FFI
Arena allocationwith_arena, parent-arena escape
Rc smart pointerShared ownership, refcount semantics

Metaprogramming

ExampleTeaches
Writing a stratumAdding a keyword via @stratum
Strata as design solventBuilding 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 run

If 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.