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.
The Mixins module helps to share functionality among classes:
--!strict
Mixin.apply(targetClass: table, mixin: table): nil
Applies a mixin to a class, adding methods and properties.
targetClass
→ The class table returned by RoClass.new(...):build()
.mixin
→ Table containing methods and properties to apply.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.