Skip to main content

Classes (Sanctuaries)

In Soul, classes are called “sanctuaries” and provide a blueprint for creating objects.

Basic Class Definition

The __init__ method is the constructor and is called automatically when creating new instances with .new().

Instance Properties

Properties are dynamically added to instances:

Methods

Methods are functions defined within a sanctuary:

Inheritance

Classes can inherit from other classes using extends:

Using Super

The super keyword accesses parent class methods:

Traits (Oasis)

Traits provide a way to share behavior across multiple classes without inheritance:

Static Methods

Static methods belong to the class itself, not instances:

Property Access

Getters and Setters Pattern

Composition Over Inheritance

Sometimes composition is better than inheritance:

Design Patterns

Singleton Pattern

Builder Pattern

Best Practices

Keep Classes Focused

Each class should have a single, well-defined responsibility.

Favor Composition

Use composition when you need to combine behaviors from multiple sources.

Use Traits Wisely

Traits are great for cross-cutting concerns like logging or timestamps.

Encapsulate Data

Use methods to control access to internal state rather than direct property access.