samples/iyi/shapes.iyi
The first program with a trait of its own - shapes that answer their.
# The first program with a trait of its own - shapes that answer their # area, held in one list, added up - and the arithmetic around it. # # ./bin/iyi run samples/iyi/shapes.iyi # # `hello` documents what a trait is. This is the program a person writes # once they know: two structs behind one trait, a list typed by the trait, # and the small arithmetic a program like it reaches for - a rounded float, # a sum over a block, a number parsed that may fail, a power, a shift, a loop # that ends on a `break`. Like `basics`, `inventory`, `config` and `grid`, # it documents no rule; the prelude grows only when a program in this # repository needs something (SPEC.md 0.1.0 item 3). module samples/shapes trait Shape # A requirement says so: `abstract`. A plain `def` here opens a body. abstract def area : Float64 abstract def name : String end struct Circle def initialize(@radius : Float64) end end impl Shape for Circle def area : Float64 3.14159 * @radius * @radius end def name : String "circle" end end struct Rect def initialize(@width : Float64, @height : Float64) end end impl Shape for Rect def area : Float64 @width * @height end def name : String "rect" end end # 1. One list, two kinds, each asked the same question. shapes = [Circle.new(1.0), Rect.new(2.0, 3.0)] of Shape shapes.each { |shape| puts "#{shape.name}: #{shape.area.round(2)}" } # => circle: 3.14 # => rect: 6.0 puts shapes.sum { |shape| shape.area }.round(1) # => 9.1 puts shapes.max_by { |shape| shape.area }.name # => rect # 2. Numbers that arrive as text, some of them wrong. inputs = ["12", "x", "7", "-3"] parsed = inputs.map { |text| text.to_i? } puts parsed # => [12, nil, 7, -3] puts parsed.compact.sum # => 16 puts parsed.compact.map(&.abs) # => [12, 7, 3] # 3. Powers, shifts, and a loop that decides when to stop. puts 2 ** 10, 2.0 ** -2 # => 1024, 0.25 puts 1_i64 << 40 # => 1099511627776 puts 1024 >> 3 # => 128 side = 0 loop do side += 1 break if side * side > 50 end puts side # => 8
Press Run.
The pane no longer holds samples/iyi/shapes.iyi. Run executes iyi-shapes.wasm, the module the compiler built from the recorded file, so your edit is not in this run: there is no iyi compiler in this page. How this works
recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19
You can type in the pane: nothing here compiles, so what you type cannot run, and the page says which bytes it is running the moment the text stops being the recorded file. There is no standard input, no filesystem and no threads on this target, and one sample fails on purpose for exactly that reason.
Run executes iyi-shapes.wasm, the module the compiler built from this file, in your browser. How this works
Try it on your machine
Copy the program into shapes.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.
$ iyi run shapes.iyi