Skip to content

Yet another class library for Lua, because there aren't enough already. ¯\_(ツ)_/¯

Notifications You must be signed in to change notification settings

Possseidon/lua-class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-class

Yet another class library for Lua, because there aren't enough already. ¯\_(ツ)_/¯

Usage

It follows a small example showing its basic usage.

Personally I use PascalCase for my class names, which is fairly common in other languages as well. Of course, feel free to use whichever convention suits you best.

local class = require "class"

-- Creating the class:

local Value = class("Value")

function Value:create(value)
  self._value = value
end

function Value:get()
  return self._value
end

function Value:set(value)
  self._value = value
end

-- Creating an instance:

local value = Value("Hello World!")

print(value:get()) --> Hello World!

value:set("How are you?")

print(value:get()) --> How are you?

A class Value with simple create (constructor), get and set methods.

I generally put each class in its own file and name the file accordingly, similar to how Object.lua and Event.lua look.


Library

  • The core library.
  • Can be used standalone.
  • Supports creation of classes and inheritance.
  • Can be used as a base class.
  • Enables the use of properties using custom get and set functions.
  • Comes with useful helper functions to create properties with common get and set functions.
  • Provides a simple multicast event.
  • Handlers can be added and removed with the add and remove methods.
  • Calls all handlers in the order they were added, when the event is called.

Examples

  • Shows basic usage.
  • A simple class Value with Value:get() and Value:set(value) methods.
  • Customize instance creation with a MyClass:create() method.
  • Creating instance fields.
  • Creating a base class Animal and a subclass Cat.
  • Virtual and abstract methods.
  • Calling base methods in a derived method.
  • Checking if something is a class or an object using class.isClass() and class.isObject().
  • Safely turning any value into its class (or nil) using class.of().
  • Checking the inheritance hierachy of classes and objects using the comparison operators.
  • Customize what happens, when a certain class is inherited from.

About

Yet another class library for Lua, because there aren't enough already. ¯\_(ツ)_/¯

Topics

Resources

Stars

Watchers

Forks

Languages