Skip to content

Commit dcf0b2d

Browse files
author
Miqueas
committed
New Self release (v0.2.0)!
This is just an API breaking change: - Self no longer returns a function when `require()`'d And some minor internal changes: - Updated copyright year - Renamed internal table `Object` to `Class` The test file were also updated to match the new changes
1 parent 852621b commit dcf0b2d

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

Self.lua

+20-40
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
zlib License
77
8-
Copyright (c) 2020 - 2022 Miqueas Martinez
8+
Copyright (c) 2020 - 2024 Miqueas Martinez
99
1010
This software is provided 'as-is', without any express or implied
1111
warranty. In no event will the authors be held liable for any damages
@@ -89,15 +89,15 @@ end
8989

9090
--- Parent class for all classes
9191
--- @class Object
92-
local Object = {}
92+
local Class = {}
9393

9494
--- Creates a class
9595
--- @param def table Class "template"
9696
--- @return table
97-
function Class(def)
97+
local function createClass(def)
9898
check_arg(1, def, "table")
9999

100-
local class = setmt(def, Object)
100+
local class = setmt(def, Class)
101101

102102
return class
103103
end
@@ -108,7 +108,7 @@ end
108108
--- @param instance table The object
109109
--- @param class table The class
110110
--- @return boolean|string
111-
function Object.is(instance, class)
111+
function Class.is(instance, class)
112112
check_arg(1, instance, "table")
113113
opt_arg(2, class, "table")
114114

@@ -118,25 +118,25 @@ function Object.is(instance, class)
118118
return "Object"
119119
end
120120

121-
return mt == class or getmt(mt) == Object
121+
return mt == class or getmt(mt) == Class
122122
end
123123

124124
--- Implements all functions from one or more classes/tables given.
125125
--- Can't use this from an instance.
126126
--- @param class table The class where implement functions
127127
--- @param ... table Tables/classes with functions to import from
128128
--- @return nil
129-
function Object.implements(class, ...)
129+
function Class.implements(class, ...)
130130
check_arg(1, class, "table")
131131

132132
-- Prevent using this method from an instance
133133
err(
134-
getmt(class) == Object,
134+
getmt(class) == Class,
135135
"Trying to call 'implements' method from an instance"
136136
)
137137

138-
for iface_index, iface in ipairs({ ... }) do
139-
check_arg(iface_index, iface, "table")
138+
for index, iface in ipairs({ ... }) do
139+
check_arg(index, iface, "table")
140140

141141
for name, func in pairs(iface) do
142142
if not get(class, name) then
@@ -150,14 +150,16 @@ end
150150
--- @param class table The class to use to create the object
151151
--- @param ... any Additional arguments to pass to the class constructor
152152
--- @return table
153-
function Object.__call(class, ...)
153+
function Class.__call(class, ...)
154154
check_arg(1, class, "table")
155155

156156
-- At the moment, these two meta-fields aren't writables
157157
set(class, "__index", class)
158-
set(class, "__newindex", Object.__newindex)
158+
set(class, "__newindex", Class.__newindex)
159159

160160
local o = setmt({}, class)
161+
162+
err(o.new ~= nil, "This class doesn't have a constructor method")
161163
o:new(...)
162164

163165
return o
@@ -167,50 +169,28 @@ end
167169
--- @param class table The object
168170
--- @param key string The key
169171
--- @return any
170-
function Object.__index(class, key)
171-
return get(class, key) or get(Object, key)
172+
function Class.__index(class, key)
173+
return get(class, key) or get(Class, key)
172174
end
173175

174176
--- Setter
175177
--- @param class table The object
176178
--- @param key string The key
177179
--- @param val any The value
178180
--- @return nil
179-
function Object.__newindex(class, key, val)
181+
function Class.__newindex(class, key, val)
180182
local mt = getmt(class)
181183
local _val = get(class, key) or get(mt, key)
182184

183-
if mt == Object then
185+
if mt == Class then
184186
-- From a class
185187
set(class, key, val)
186188
else
187189
-- From an instance
188-
err(_val, "Field '%s' doesn't exists.", key)
190+
err(_val ~= nil, "Field '%s' doesn't exists.", key)
189191
err(type(_val) ~= "function", "Trying to overwrite method '%s'.", key)
190192
set(class, key, val)
191193
end
192194
end
193195

194-
Object.new = Object.__call
195-
196-
--- Handles what will be returned when "require()" this library.
197-
--- See the documentation for more details.
198-
--- @param g_constructor boolean Enable/disable global constructor
199-
--- @param g_object boolean Enable/disable global `Object`
200-
--- @return function, Object|nil
201-
return function (g_constructor, g_object)
202-
opt_arg(1, g_constructor, "boolean")
203-
opt_arg(2, g_object, "boolean")
204-
205-
if g_constructor then
206-
if not _G.New then
207-
_G.New = Object.new
208-
end
209-
end
210-
211-
if g_object then
212-
return Class, Object
213-
end
214-
215-
return Class
216-
end
196+
return createClass

Test.lua

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
local Class, Object = require("Self")(true, true)
2-
local Person = Class {
3-
name = '',
1+
local class = require("Self")
2+
local Person = class {
3+
name = "",
44
age = 0
55
}
66

@@ -14,12 +14,11 @@ function Person:greet()
1414
end
1515

1616
local a = Person("A", 34)
17-
local b = New(Person, "B", 56)
18-
local c = Object.new(Person, "C", 86)
17+
local b = Person("B", 56)
18+
local c = Person("C", 86)
1919

2020
print(a:is())
2121
print(b:is(Person))
22-
print(a:is(Object))
2322

2423
-- Pretty print with Luvit
2524
if p then

0 commit comments

Comments
 (0)