Classes and objects

Browser runtime: loading…

Sapphire classes use attributes for state, methods for behavior, inheritance for specialization, and modules or abstract classes when you want stronger structure. Each runnable snippet uses the same WASM engine as the playground.

Inheritance

Subclasses use < Parent. Call overridden methods with Ruby-style super to forward the same arguments.

shapes.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Private and class methods

Use defp for helpers that should only be called inside the class. A self { ... } block defines methods on the class itself, which is handy for factories.

account.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Abstract classes

Use abstract class and abstract def when subclasses must share a base type and implement specific behavior before they can be instantiated.

abstract.spr
Press Run to execute this snippet. ⌘↵ runs from the editor.

Imports and local projects

The browser runner executes one file at a time, but local Sapphire programs can split code across files with relative imports:

import "./geometry/point"
import "../shared/helpers"

Top-level classes and functions from imported files become available to the importing file.