Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fraktality/sloop
Feature-complete OOP
https://github.com/fraktality/sloop
Last synced: 7 days ago
JSON representation
Feature-complete OOP
- Host: GitHub
- URL: https://github.com/fraktality/sloop
- Owner: Fraktality
- License: mit
- Created: 2016-11-22T18:44:23.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-30T20:14:05.000Z (over 7 years ago)
- Last Synced: 2023-10-26T08:58:57.023Z (about 1 year ago)
- Language: Lua
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sloop
Simple and fast OOP model for Lua.## Usage
Specify class members by enclosing them in curly braces:
```lua
local Person = Class{
Say = function(self, phrase)
print(self.name .. ':', phrase)
end;
}
```Instantiate a class instance by calling the class:
```lua
Person():Say('Hello, world!')
```Specify parent classes by enclosing them in parenthesis before the class body:
```lua
local PersonWhoEatsBagels = Class(Person, BagelEater){}
```Name a method `Init` to make it a constructor:
```lua
local PersonWhoEatsBagels = Class(Person, BagelEater){ -- Inherits from Person & BagelEater
Init = function(self, name, bagelCount)
rawset(self, 'name', name)
rawset(self, 'bagelCount', bagelCount)
end;
}
```Tag methods are detected and handled automatically:
```lua
local Number = Class{
Init = function(value)
self.value = value
end;
__add = function(op0, op1)
return op0.value + op1.value
end;
__tostring = function(self)
return tostring(self.value)
end;
}print(Number(1) + Number(2)) --> 3
```