https://github.com/rmuskovets/luaclass
luaclass is a library for easy OOP in Lua.
https://github.com/rmuskovets/luaclass
Last synced: 2 months ago
JSON representation
luaclass is a library for easy OOP in Lua.
- Host: GitHub
- URL: https://github.com/rmuskovets/luaclass
- Owner: RMuskovets
- Created: 2020-05-29T15:07:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-25T13:04:50.000Z (over 4 years ago)
- Last Synced: 2023-03-21T10:56:26.134Z (about 2 years ago)
- Language: Lua
- Size: 29.3 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# luaclass 2.0
"Vanilla" Lua doesn't have any mechanisms to do OOP-style programming.
With this library, I'm trying to add them using a couple of functions: `class` and `extend`.
These are pretty straightforward and I tried to write readable code.## Examples
* Simple class:
```lua
local class = require 'class'
local Foo = class {
init = function (self) print 'Foo.init' end
}
local obj = Foo() -- outputs Foo.init
```* Class with a field:
```lua
local class = require 'class'
local Foo = class {
bar = 0,
init = function (self) self.bar = 1 end
}
local obj = Foo()
print(obj.bar) -- outputs 1
```* Class with a method:
```lua
local class = require 'class'
local Foo = class {
bar = function (self) print 'Foo.bar' end
}
Foo():bar() -- outputs Foo.bar
```* `Foo extends Bar`:
```lua
local class = require 'class'
local Foo = class {
init = function (self) print 'Foo.init' end
}
local Bar = class.extend(Foo) {
init = function (self) print 'Bar.init' end
}
Bar() -- outputs Bar.init
```* Superclass constructor invocation:
```lua
local class = require 'class'
local class = require 'class'
local Foo = class {
init = function (self) print 'Foo.init' end
}
local Bar = class.extend(Foo) {
init = function (self) self.super.init() print 'Bar.init' end
}
Bar() -- outputs Foo.initBar.init
```## Docs (WIP)
### Module `class`
* **class(cls, parent?)** creates a class based on `cls`, optionally with superclass `parent`* **extend(parent)** is a two-step `class(cls, parent)`: **`extend(Foo) {}` is the same as `class({}, Foo)`**
* **static(smth)** marks a field as static: `{ static_field = static(nil) }`