Skip to main content

Classes

Mux provides object-oriented programming through classes and interfaces (traits).

Basic Class Definition

basic_class.mux
Loading...

Key Points:

  • Fields must have explicit types (no auto inference)
  • Methods use self to access instance fields
  • Methods follow same rules as regular functions

Class Instantiation

Classes use the .new() method pattern:

class_instantiation.mux
Loading...

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:

interfaces.mux
Loading...

Implementing Interfaces

Use the is keyword to implement interfaces:

implementing_interfaces.mux
Loading...

Note: Use is instead of implements (like Java). Multiple interfaces separated by commas.

Methods

Instance Methods

Access instance data via self:

instance_methods.mux
Loading...

Methods with Unused Parameters

class_unused_params.mux
Loading...

Static Methods with common

The common keyword declares static (class-level) methods:

static_methods.mux
Loading...

common vs const

KeywordPurposeUsage
commonStatic methods and factory functionsClassName.method()
constImmutable constantsconst int MAX = 100

Key Differences:

  • Instance methods (no keyword) operate on self and require an instance
  • Static methods (common) have no self and are called on the class
  • Const fields are immutable instance/class fields, not methods
  • Static methods cannot access instance fields (no self context)

Constants in Classes

Classes can have constant (immutable) fields:

class_constants.mux
Loading...

Const Enforcement:

  • Cannot reassign: self.MAX_RETRIES = value -> ERROR
  • Cannot increment/decrement: self.MAX_RETRIES++ -> ERROR
  • Use const for fields that shouldn't change after initialization

Generic Classes

Classes can be generic over type parameters:

class_generics.mux
Loading...

See Generics for more details.

Interface Dispatch (Static)

Mux uses static dispatch for interfaces - no runtime vtable lookup:

static_dispatch.mux
Loading...

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

  1. Fields must be explicitly typed - No auto for class fields
  2. Use interfaces for polymorphism - Define common behavior
  3. Use common for factory methods - Create instances with pre-populated data
  4. Keep classes focused - Single responsibility principle
  5. Use const for immutable fields - Prevent accidental modification
  6. Leverage generic classes - Reusable data structures
  7. 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