Classes
Mux provides object-oriented programming through classes and interfaces (traits).
Basic Class Definition
Key Points:
- Fields must have explicit types (no
autoinference) - Methods use
selfto access instance fields - Methods follow same rules as regular functions
Class Instantiation
Classes use the .new() method pattern:
Design Note: Mux uses explicit .new() rather than direct constructor calls to distinguish class instantiation from function calls and enum variant construction. The .new() method will always instantiate a new object with all default "zero" values for fields, and then you can set fields afterward. This is a simple and consistent pattern for object creation.
The "Mux" style for a constructors is to use a common (see below) method that creates and initializes the object, rather than defining a special constructor syntax. This allows for more flexible object creation patterns while keeping the core class definition simple.
Interfaces (Traits)
Interfaces define required methods that classes must implement:
Implementing Interfaces
Use the is keyword to implement interfaces:
Note: Use is instead of implements (like Java). Multiple interfaces separated by commas.
Methods
Instance Methods
Access instance data via self:
Methods with Unused Parameters
Static Methods with common
The common keyword declares static (class-level) methods:
common vs const
| Keyword | Purpose | Usage |
|---|---|---|
common | Static methods and factory functions | ClassName.method() |
const | Immutable constants | const int MAX = 100 |
Key Differences:
- Instance methods (no keyword) operate on
selfand require an instance - Static methods (
common) have noselfand are called on the class - Const fields are immutable instance/class fields, not methods
- Static methods cannot access instance fields (no
selfcontext)
Constants in Classes
Classes can have constant (immutable) fields:
Const Enforcement:
- Cannot reassign:
self.MAX_RETRIES = value-> ERROR - Cannot increment/decrement:
self.MAX_RETRIES++-> ERROR - Use
constfor fields that shouldn't change after initialization
Generic Classes
Classes can be generic over type parameters:
See Generics for more details.
Interface Dispatch (Static)
Mux uses static dispatch for interfaces - no runtime vtable lookup:
Why Static Dispatch?
- Zero cost: No pointer indirection, direct function calls
- Inlining: LLVM can inline interface methods
- Optimization: Better branch prediction, no indirect jumps
The tradeoff: interfaces cannot be added to types from other modules (no "extension traits").
Best Practices
- Fields must be explicitly typed - No
autofor class fields - Use interfaces for polymorphism - Define common behavior
- Use
commonfor factory methods - Create instances with pre-populated data - Keep classes focused - Single responsibility principle
- Use
constfor immutable fields - Prevent accidental modification - Leverage generic classes - Reusable data structures
- Prefer static dispatch - Better performance than dynamic dispatch
See Also
- Generics - Generic classes and type parameters
- Interfaces - Built-in interfaces for common operations
- Memory - Reference counting and object lifecycle
- Functions - Method definitions