-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleclass.d.lua
More file actions
348 lines (274 loc) · 8.18 KB
/
Copy pathsimpleclass.d.lua
File metadata and controls
348 lines (274 loc) · 8.18 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
---@meta simpleclass
---Types declare for simpleclass module
---@class simpleclass
local sc = {}
---@class class<o>
---@field __proto o
---@field __classname string
---@class object
---@class object.class : class<object>
---@operator call: object
---@class interface : string[]
---@class super<cls, obj>
---@field self obj
---@field __class cls
---@field [string] unknown
-----------------------------------------------------------------------------------------------------
---@class class<o>
local c
---Create a instance
---@generic o
---@return o
function c:new() end
---Check if the class extends the base class
---@param base class
---@return boolean
function c:isExtends(base) end
---Check if the class implements the interfaces
---@param ... interface
---@return boolean ok
---@return integer? arg_index if not ok
function c:isImplements(...) end
---Convert the object to a string
---@return string
function c:toString() end
-----------------------------------------------------------------------------------------------------
---The base class of all classes
---@class object.class
local o = {__classname = "object"}
---@return object
function o:new() end
---Check if the class implements the interface
---If not, it returns the name of a method missing implements
---@param iface interface
---@return boolean ok
---@return string? meth
function o:isImpl(iface) end
---Clone an object (table)
---@generic T:table
---@param obj T
---@param isDeep? boolean Default `true`
---@return T
function o.clone(obj, isDeep) end
---@param self object
---@return string
function o.__tostring(self) end
---@class object
---@field __class object.class
o.__proto = {}
---Get the class of the object
---@return class
function o.__proto:getClass() end
---Check if the object is an instance of the class or interface
---@param cls class|interface|"table"
---@return boolean
function o.__proto:isInstance(cls) end
---Convert the object to a string
---@return string
function o.__proto:toString() end
---Clone the object
---@param isDeep? boolean Default `true`
---@return object
function o.__proto:clone(isDeep) end
o.__proto.is = rawequal
-----------------------------------------------------------------------------------------------------
---@alias _ClassDefiner<T> fun(tbl: table): T
---@class _ClassCreator<T>
---@field [string] fun(cc: _ClassCreator<T>, tbl: table): T
local cc = {}
---Single inheritance keyword
---@generic T
---@param basename? string
---@return _ClassCreator<T>|_ClassDefiner<T>
function cc:extends(basename) end
---Implements the interfaces
---@generic T
---@param ... interface
---@return _ClassCreator<T>|_ClassDefiner<T>
function cc:implements(...) end
cc.impl = cc.implements
---Define the class body
---@generic T
---@param tbl table
---@return T
function cc:def(tbl) end
-----------------------------------------------------------------------------------------------------
---@alias _InterfaceDefiner<T> fun(lst: string[]): T
---@class _InterfaceCreator<T>
local ic = {}
---Extend the interface with other interfaces
---@generic T
---@param ... interface
---@return _InterfaceCreator<T>
function ic:extends(...) end
-----------------------------------------------------------------------------------------------------
---Get the type of a value, considering classes as special types
---eg:
---```lua
---simpleclass.type(Eagle()) => Eagle
---simpleclass.type("Hello") => "string"
---```
---@generic o:object
---@param obj o
---@return class<o> cls
function sc.type(obj) end
---Equivalent to `type` function
---@param v any
---@return type type
function sc.type(v) end
---Define a new class
---eg:
---```lua
---class "MyClass" : MyBase {
--- __init = function(self)
--- super():__init()
--- end
---}
---```
---or anonymous:
---```lua
---local cls = class {}
---```
---@generic T:string
---@param name? `T`.class
---@return _ClassCreator<T>|_ClassDefiner<T>
function sc.class(name) end
---@generic T:table
---@param body T
---@return T
function sc.class(body) end
---Define a new interface
---@generic I:string
---@param name I.`I`
---@return _InterfaceCreator<I>|_InterfaceDefiner<I>
function sc.interface(name) end
---@param body? string[]
---@return interface
function sc.interface(body) end
--[[To call superclass methods
eg: `super(cls, self):__init()`
> If `debug` is available, it can automatically get the context,
> so you can omit parameters in an instance method.
> eg: `super():__init()` ]]
---@generic cls:class, obj:object
---@param cls cls
---@param obj? obj
---@return super<cls, obj>
function sc.super(cls, obj) end
---Check if the object is an instance of the class or interface
---(also compatible with lua type)
---@param obj object
---@param cls class|interface|type
---@return boolean
function sc.isinstance(obj, cls) end
---Check if `type(v) == T`
---@param v any
---@param T type
---@return boolean
function sc.isinstance(v, T) end
sc.issubclass = c.isExtends
sc.object = o
---The environment of the classes and interfaces
---@type {object: object.class, [string]: class|interface}
sc._ENV = {object = o}
---@alias alias.target function
---@alias alias.origin table<string, alias.target>
---Create an alias or a partial function for a method.
------
---To create an alias, do like this:
---```lua
---class "Person" {
--- walk = function(self) end;
--- alias.move :walk();
---}
---print(Person().move == Person().walk) --> true
---```
------
---To create a partial function, pass some arguments to the alias:
---```lua
---class "Calculator" {
--- add = function(a, b) return a + b end;
--- alias.add1 .add(1);
---}
---print(Calculator.add1(2)) --> 3
---```
---> A positional partial can also be created outside a class body:
---> ```lua
---> partial = alias(func, ...)
---> ```
------
---## 「:」vs「.」
---It’s simple:
---If a method is expected to be called with “:”, then its alias must also use the “:” syntax;
---conversely, if it’s expected to be called with “.”, then its alias should use the “.” syntax.
---@alias simpleclass.alias table<string, alias.origin>|fun(function, ...):function
---@type simpleclass.alias
sc.alias = {}
---@alias property function
---Declare a property in the class body.
---> eg: `property.my_prop;`
---Once declared, the property can bind getter and setter methods.
---A getter method should be named as `['get.my_prop']`, while setter as `['set.my_prop']`.
------
---Here is a basic usage:
---```lua
---class "MyClass" {
--- property.six;
--- ['get.six'] = function(self) return 6 end;
---}
---print(MyClass().six) --> 6
---```
---@alias simpleclass.property table<string, property>
---@type simpleclass.property
sc.property = {}
-----------------------------------------------------------------------------------------------------
---@alias simpleclass.I_FEATURE
---| "general" Full interface feature.
---| "nocheck" Skip interface checking.
---| "lexical" Only interface syntax. (for LS analysis)
---@alias simpleclass.FIELD
---| "class"
---| "super"
---| "alias"
---| "interface"
---| "type"
---| "object"
---| "property"
---| "isinstance"
---| "issubclass"
---Whether to register classes as global variables automatically.
---Default true when import globally, false otherwise. Could be overridden in runtime.
---@type boolean
sc.AUTO_GLOBAL = false
---Interface feature to use, meaningless if interface module not included.
---**Warning**: switch in "general" & "nocheck" is safe, BUT "lexical" skipping definition is irreversible.
---@type simpleclass.I_FEATURE
sc.I_FEATURE = "general"
---Import fields from simpleclass module to environment.
---eg:
---```
---simpleclass.env_import({
--- "class",
--- "super",
--- ["type"] = "cls_type"
---}, _ENV)
---```
---which means:
---```python
---from simpleclass import
--- class,
--- super,
--- type as cls_type
---```
---@param fields {[simpleclass.FIELD]?: string, [integer]?: simpleclass.FIELD}
---@param env? table Default to `_G` if missing.
function sc.env_import(fields, env) end
class = sc.class
super = sc.super
interface = sc.interface
isinstance = sc.isinstance
issubclass = sc.issubclass
object = sc.object
property = sc.property
-----------------------------------------------------------------------------------------------------
return sc