Sandbox: Lua: Namespace - ov-studio/Vital.sandbox GitHub Wiki

» Overview

This module provides a robust implementation of namespaces and classes in Lua, offering:

  • Controlled variable and handler scope management.
  • Code organization into logical groups.
  • Prevention of naming collisions.
  • Support for public and private members.
  • Lightweight class simulation with instance management.

» Key Features

  • Namespace and class creation with scope control.
  • Public and private member support.
  • Nested class and namespace capabilities.
  • Safe instance management.

» APIs

  • namespace:create() - shared

    Creates the specified namespace.
    🚨 Always ensure to localize the namespace instance to avoid being exposed to global scope.

    --Note: public & private members can be appended later via cNamespace.public & cNamespace.private respectively.
    local namespace: cNamespace = namespace:create(
       string: name, --Name of your namespace
       table: parent --Optional: Any non class table that you wish to utilize for creating the namespace
    )
  • namespace:destroy() - shared

    Destroys an existing namespace.
    🚨 Destroying namespace will also destroy any further classes & their instances located within it.

    local bool: result = namespace:destroy(
       string: name --Name of your namespace
    )
  • class:getName() - shared

    Retrieves class's name.

    local string: name = self:getName()
  • class:create() - shared

    Creates the specified class.
    🚨 Always ensure to localize the class instance to avoid being exposed to global scope.

    --Note: public & private members can be appended later via cClass.public & cClass.private respectively.
    local class: cClass = class:create(
       string: name, --Name of your class
       table: parent, --Optional: Any non class table that you wish to utilize for creating the class
       string: namespace --Optional: Valid namespace that you prefer to scope the class within. If not specified the instance will be created within the global scope
    )
  • class:destroy() - shared

    Destroys the specified class.
    🚨 Destroying class also destroys all of its belonging instances.

    local bool: result = class:destroy(
       class: instance
    )
  • class:isInstance() - shared

    Verifies whether the table is an instance of the specified class.

    local bool: result = self:isInstance(
       class <instance>: cInstance
    )
  • class:createInstance() - shared

    Creates an instance of the specified class.

    local class <instance>: cInstance = self:createInstance()
  • class:destroyInstance() - shared

    Destroys specified instance.

    local bool: result = cInstance:destroyInstance()
⚠️ **GitHub.com Fallback** ⚠️