RoClass

Mixins

Mixins is a core module in RoClass that allows you to add reusable functionality to classes.
It provides a simple system to apply methods, properties, or behaviors across multiple classes without inheritance.


Table of Contents


Overview

The Mixins module helps to share functionality among classes:


API

Mixin.apply(targetClass: table, mixin: table): nil

Applies a mixin to a class, adding methods and properties.


Examples

Creating and applying a mixin

local Mixin = RoClass.Mixin

-- Define a mixin
local HealthMixin = {
    TakeDamage = function(self, dmg)
        self.Health -= dmg
    end,
    Heal = function(self, amount)
        self.Health += amount
    end
}

-- Create a class
local Player = RoClass.new("Player")
    :constructor(function(self)
        self.Health = 100
    end)
    :build()

-- Apply the mixin
Mixin.apply(Player, HealthMixin)

local p1 = Player.new()
p1:TakeDamage(25)
print(p1.Health) -- 75
p1:Heal(10)
print(p1.Health) -- 85


This documentation is part of RoClass. For more examples and detailed guides, see RoClass Docs.