Skip to content
Praron edited this page Feb 21, 2016 · 4 revisions

30log provides a basic support for mixins. This is a powerful concept that can be used to share the same functionality across different classes, especially when they are unrelated.

30log implements mixins as tables containing a set of functions prototyped as methods. A mixin is included in a class using class:include().

-- A simple Geometry mixin
local Geometry = {
  getArea = function(self) return self.width * self.height end
}

-- Let us define two unrelated classes
local Window = class ("Window", {width = 480, height = 250})
local Button = class ("Button", {width = 100, height = 50, onClick = false})

-- Include the "Geometry" mixin in Window and Button classes
Window:include(Geometry)
Button:include(Geometry)

-- Let us define instances from those classes
local aWindow = Window()
local aButton = Button()

-- Instances can use functionalities brought by Geometry mixin.
print(aWindow:getArea()) -- outputs 120000
print(aButton:getArea()) -- outputs 5000

class:includes() returns true when a class includes a specific mixin.

print(Window:includes(Geometry)) -- true
print(Button:includes(Geometry)) -- true
Clone this wiki locally