Skip to content

Blocks and Closures

Both forms are equivalent:

# Brace form (conventionally single-line)
[1, 2, 3].map { |x| x * 2 }
# do/end form (conventionally multi-line)
[1, 2, 3].map do |x|
x * 2
end

Methods declare a typed block parameter with &block:

def map(&block: (T) -> R): Array[R]
def each(&block: (T) -> Void): Void

Receiver blocks allow self inside a block to be statically typed to a specific receiver, enabling DSL patterns. The receiver type is declared with with:

def html(&block: () -> Void with HtmlBuilder): HtmlBuilder
builder = HtmlBuilder.new
yield builder
builder
end

yield is used to invoke the block. For receiver blocks, the first argument to yield becomes self inside the block. Subsequent arguments are the block’s parameters.

# No block args — only a receiver
yield builder
# Block args + receiver — receiver is always first
yield builder, item

The type (A) -> B with C maps to: receiver C as self, argument A, return B.

def each_item(&block: (Item) -> Void with Context): Void
ctx = Context.new
@items.each do |item|
yield ctx, item
end
end
page = html do # self is HtmlBuilder
head do # self is HeadBuilder
title "Hello"
end
body do # self is BodyBuilder
p "World"
end
end

Lambdas use the stabby arrow syntax:

double = ->(x: Int) { x * 2 }
double = ->(x: Int): Int { x * 2 } # with explicit return type

Lambdas and stored blocks share the same type — the function signature (T) -> R. Call a stored lambda or block with .call:

fn = ->(x: Int) { x * 2 }
fn.call(5) # 10

A stored lambda can be passed as the block argument to a method using &:

double = ->(x: Int) { x * 2 }
[1, 2, 3].map(&double) # same as .map { |x| x * 2 }

The variable’s type must be compatible with the method’s &block parameter type.

A &block parameter received by a method can be passed on the same way:

def apply(items: Array[Int], &block: (Int) -> Int): Array[Int]
items.map(&block)
end

&:symbol is shorthand for a block that calls the named method on each element:

["alice", "bob"].map(&:upcase) # same as .map { |s| s.upcase }
users.select(&:active?) # same as .select { |u| u.active? }

The compiler resolves the method from the element type at compile time — if the method does not exist on that type, it is a compile error. Arguments cannot be passed with this shorthand; use a full block if arguments are needed.

method(:name) captures a reference to a method as a callable value of the appropriate function type:

def double(x: Int): Int
x * 2
end
[1, 2, 3].map(&method(:double)) # same as .map { |x| double(x) }

Instance methods can be captured on a specific receiver:

[1, 2, 3].map(&formatter.method(:format))

Amethyst follows Ruby’s return semantics:

  • return inside a block returns from the enclosing method
  • return inside a lambda (->) returns from the lambda itself
  • next exits the current block iteration (local return from block)
  • break exits the method that yielded the block
Keyword Behaviour in a block Mechanism
return Returns from the enclosing method ControlFlowReturnException
next Returns from the block (local return) Normal IL ret from the block
break Exits the method that called yield ControlFlowBreakException

Lambdas never throw control flow exceptions — return compiles to a normal IL ret.

Non-local return and break from blocks are implemented via exceptions under the hood.

  • return value inside a block compiles to throw ControlFlowReturnException(target_id, value)
  • break value inside a block compiles to throw ControlFlowBreakException(target_id, value)
  • Each method that uses yield is assigned a unique target_id at compile time
  • The yield call site wraps in a try/catch and only intercepts if target_id matches, otherwise re-throws

This ensures correct behaviour with nested blocks:

def outer
inner do
return "hello" # targets outer's frame ID — passes through inner's yield uncaught
end
end
def inner(&block: () -> Void): Void
yield # catches ControlFlowReturnException only if target_id matches inner — it doesn't
end