Module Class

Reusable class creation patterns.

Module Status: Polishing.

Usage:

    local Class = require('__eradicators-library__/erlib/lua/Class')()
    

Class Creation

SimpleClass(initializer[, finalizer]) Creates a new simple class with build in initialization-by-call.
SwitchCaseClass(analyzer, cases) A class initializer based on a Meta.SwitchCase.

SimpleClass built-in methods

MySimpleClass(...) Instantiates a new object of the class by passing it through the initializer and the finalizer.
MySimpleClass.reclassify(object) Unconditionally attaches this classes metatable to an object.

SwitchCaseClass built-in methods

MySwitchCaseClass(...) Instantiates a new object of the class by calling the appropriate case function.
MySwitchCaseClass.reclassify(object) Unconditionally attaches this classes metatable to an object.


Class Creation

SimpleClass(initializer[, finalizer])
Creates a new simple class with build in initialization-by-call. Can be easily extended after creation.

Parameters:

  • initializer function When a SimpleClass(...) is called directly then all arguments are passed to the initializer(...). The expected output is an instantiated ClassObjectWithoutMetatable or nil. If an object was return then the classes metatable will be automatically attached.
  • finalizer function If a finalizer is given and the initializer did not return nil, then the finalizer will be called called with the ClassObjectWithMetatable that was produced by the initializer. The return value of the finalizer is irrelevant. (optional)

Usage:

    -- First define a new class.
    local MyClass = Class.SimpleClass(
      -- The initializer must return a table.
      function(number,name)
        return {value = number, name = name}
        end,
      -- The finalizer can access all class methods.
      -- Even if they're defined later!
      function(object)
        if object.value < 5 then
          object:add(5)
          end
        end)
    
    -- Now you should add some methods to the class.
    function MyClass:add (x)
      self.value = self.value + x
      end
    function MyClass:show()
      print(('You have %s %s.'):format(self.value,self.name))
      end
    
    -- And instantiate objects
    local fruits = MyClass(42,'bananas')
    fruits:show()
    > You have 42 bananas.
    
    -- Notice how the finalizer will call :add()
    local vegetables = MyClass(1,'tomatos')
    vegetables:show()
    > You have 6 tomatos.
    
SwitchCaseClass(analyzer, cases)
A class initializer based on a Meta.SwitchCase.

Note: If the case function returns a ClassObjectWithMetatable then the metatable for this class will not be attached. A possible usecase for this is for example an "invalid" case that returns an object that doesn't instantly crash class methods but is still not a full member.

Parameters:

Usage:

    local MyClass = Class.SwitchCaseClass(
      function(input)
        if type(input) == 'table' then
          return 'invalid'
          end
        return type(input)
        end,
      {
        string  = function(x) return {value = tostring(x),source = 'string '} end,
        number  = function(x) return {value =          x ,source = 'number '} end,
        default = function(x) return {value =          0 ,source = 'default'} end,
        invalid = function(x) return setmetatable(
          {valid=false},
          {__index = function()
            error('Error! Reading data from an invalid object is not allowed!')
            end }
          ) end
      })
    
    function MyClass:show()
      print(('This was a %s and now has value %s.'):format(self.source,self.value))
      end
    
    local MyObjectA = MyClass('42')
    MyObjectA:show()
    > This was a string  and now has value 42.
    
    local MyObjectB = MyClass(24)
    MyObjectB:show()
    > This was a number  and now has value 24.
    
    local MyObjectC = MyClass(true)
    MyObjectC:show()
    > This was a default and now has value 0.
    
    local MyObjectD = MyClass.reclassify {value = 17, source = 'secret operation'}
    MyObjectD:show()
    > This was a secret operation and now has value 17.
    
    local MyObjectE = MyClass({dummy=0})
    MyObjectE:show()
    > Error! Reading data from an invalid object is not allowed!
    

SimpleClass built-in methods

MySimpleClass(...)
Instantiates a new object of the class by passing it through the initializer and the finalizer. Finally it returns the object.

Parameters:

  • ... AnyValue Any data needed to initialize a ClassObject.

Returns:

    ClassObjectWithMetatable
MySimpleClass.reclassify(object)
Unconditionally attaches this classes metatable to an object. Allows you to skip the normalization process if you already know that the object is a valid member of this class.

Parameters:

  • object table A ClassObject that has lost it's metatable.

Returns:

    ClassObjectWithMetatable

SwitchCaseClass built-in methods

MySwitchCaseClass(...)
Instantiates a new object of the class by calling the appropriate case function. Finally it returns the object.

Parameters:

  • ... AnyValue Any data needed to initialize a ClassObject.

Returns:

    ClassObjectWithMetatable
MySwitchCaseClass.reclassify(object)
Unconditionally attaches this classes metatable to an object. Allows you to skip the normalization process if you already know that the object is a valid member of this class.

Parameters:

  • object table A ClassObject that has lost it's metatable.

Returns:

    ClassObjectWithMetatable
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19