Multiple Handlers
A cell can have multiple handlers. Each is like a public method. Handlers can call each other.
Example 010 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Rectangle has no face and no memory. Handlers: area (line 22), perimeter (line 27) and describe (line 32).
The source
010_multiple_handlers.cell
// ============================================================================
// 010 — Multiple Handlers
// ============================================================================
// A cell can have multiple handlers. Each is like a public method.
// Handlers can call each other.
//
// Run: soma run 010_multiple_handlers.cell area 5 3
// Expect: 15
//
// Run: soma run 010_multiple_handlers.cell perimeter 5 3
// Expect: 16
//
// Run: soma run 010_multiple_handlers.cell describe 5 3
// Expect:
// Rectangle 5 x 3
// Area: 15
// Perimeter: 16
cell Rectangle {
// Compute the area of a rectangle
on area(width: Int, height: Int) {
return width * height
}
// Compute the perimeter
on perimeter(width: Int, height: Int) {
return 2 * (width + height)
}
// Handlers can call other handlers in the same cell
on describe(width: Int, height: Int) {
let a = area(width, height)
let p = perimeter(width, height)
print("Rectangle {width} x {height}")
print("Area: {a}")
print("Perimeter: {p}")
return "done"
}
}What the toolchain says
$ soma check 010_multiple_handlers.cell
✓ All checks passed.$ soma run 010_multiple_handlers.cell area 5 3
15$ soma run 010_multiple_handlers.cell perimeter 5 3
16$ soma run 010_multiple_handlers.cell describe 5 3
Rectangle 5 x 3
Area: 15
Perimeter: 16
done