iyi

Playground · Basics · 10 of 33

nil

nil is a value, and the type says whether it can appear.

samples/tour/nil.iyi · nil.wasm · 122 KB
# nil is a value, and the type says whether it can appear.

# `String?` is `String | Nil`: it may hold a string, or nothing.
def lookup(id : Int32) : String?
  id == 1 ? "Ada" : nil
end

found = lookup(1)
missing = lookup(2)

puts found
puts missing

# Before you use a maybe-nil value, check it. Inside the `if`, the
# compiler knows it is a String.
if found
  puts found.upcase
end

# Or give a default with `||`.
puts missing || "nobody"

# Calling a String method on a `String?` without checking does not
# compile. That is the whole point: a nil can never surprise you at run time.
output
Press Run.

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 nil.wasm, the module the compiler built from this file, in your browser. How this works

Try it on your machine

Copy the program into nil.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.

sh, after installing
$ iyi run nil.iyi