This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.lua
More file actions
56 lines (51 loc) · 1.88 KB
/
object.lua
File metadata and controls
56 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
--- Prototype-based objects
-- <ul>
-- <li>Create an object/class:</li>
-- <ul>
-- <li>Either, if the <code>_init</code> field is a list:
-- <ul>
-- <li><code>object/Class = prototype {value, ...; field = value, ...}</code></li>
-- <li>Named values are assigned to the corresponding fields, and unnamed values
-- to the fields given by <code>_init</code>.</li>
-- </ul>
-- <li>Or, if the <code>_init</code> field is a function:
-- <ul>
-- <li><code>object/Class = prototype (value, ...)</code></li>
-- <li>The given values are passed as arguments to the <code>_init</code> function.</li>
-- </ul>
-- <li>An object's metatable is itself.</li>
-- <li>Private fields and methods start with "<code>_</code>".</li>
-- </ul>
-- <li>Access an object field: <code>object.field</code></li>
-- <li>Call an object method: <code>object:method (...)</code></li>
-- <li>Call a class method: <code>Class.method (object, ...)</code></li>
-- <li>Add a field: <code>object.field = x</code></li>
-- <li>Add a method: <code>function object:method (...) ... end</code></li>
-- </li>
module ("object", package.seeall)
require "table_ext"
--- Root object
-- @class table
-- @name Object
-- @field _init constructor method or list of fields to be initialised by the
-- constructor
-- @field _clone object constructor which provides the behaviour for <code>_init</code>
-- documented above
_G.Object = {
_init = {},
_clone = function (self, ...)
local object = table.clone (self)
if type (self._init) == "table" then
table.merge (object, table.clone_rename (self._init, ...))
else
object = self._init (object, ...)
end
return setmetatable (object, object)
end,
-- Sugar instance creation
__call = function (...)
-- First (...) gets first element of list
return (...)._clone (...)
end,
}
setmetatable (Object, Object)