diff options
author | David Walter Seikel | 2014-03-29 20:31:10 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-29 20:31:10 +1000 |
commit | 07bbdba2da74c35bdc755e9c806fc9b06d51f8d8 (patch) | |
tree | 9dc664268288c3cf8e6dc1cd3d88effce184eca1 /ClientHamr | |
parent | Update isValid() to deal with multiple Thing copies. (diff) | |
download | SledjHamr-07bbdba2da74c35bdc755e9c806fc9b06d51f8d8.zip SledjHamr-07bbdba2da74c35bdc755e9c806fc9b06d51f8d8.tar.gz SledjHamr-07bbdba2da74c35bdc755e9c806fc9b06d51f8d8.tar.bz2 SledjHamr-07bbdba2da74c35bdc755e9c806fc9b06d51f8d8.tar.xz |
Two alternate ideas for how to do Things.
Diffstat (limited to 'ClientHamr')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 7ec7a64..3848093 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -257,6 +257,91 @@ Other Thing things are - | |||
257 | --[[ TODO | 257 | --[[ TODO |
258 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. | 258 | NOTE that skang.thing{} doesn't care what other names you pass in, they all get assigned to the thing. |
259 | 259 | ||
260 | Might be better to move skang.things.foo.value -> skang.values.module.foo | ||
261 | but keep skang.things.foo.* for the Thing metadata. | ||
262 | |||
263 | test.pre | ||
264 | skang.things.test | ||
265 | skang.things.test.foo | ||
266 | skang.things.pre_test.foo -> skang.things.test.foo | ||
267 | skang.values.test.foo | ||
268 | skang.values.pre_test.foo | ||
269 | |||
270 | thing:isValid(module) | ||
271 | skang.things[module][key]:isValid(module) | ||
272 | skang.things.test.foo:isValid(test) | ||
273 | isValid(thing, module) | ||
274 | local value = values[module._NAME][self.names[1] ] | ||
275 | |||
276 | __index(module, key) | ||
277 | local thing = things[module._NAME][key] | ||
278 | local value = values[module._NAME][key] | ||
279 | |||
280 | __newindex(module, key, value) | ||
281 | local thing = things[module._NAME][key] | ||
282 | local oldValue = values[module._NAME][key] | ||
283 | ... | ||
284 | values[module._NAME][key] = value | ||
285 | ... | ||
286 | if not thing:isValid(module) then | ||
287 | |||
288 | module(...) or module{...} | ||
289 | __call(module, ...) | ||
290 | |||
291 | new = function (module, name) | ||
292 | local result = {} | ||
293 | setmetatable(result, {__index=module}) | ||
294 | result._NAME = name | ||
295 | -- Loop through the Things in module. | ||
296 | for k, v in pairs(module.things) do | ||
297 | values[name][k] = values[module._NAME][k] | ||
298 | end | ||
299 | |||
300 | On the other hand, might be better to now think of modules as a type? | ||
301 | How much of OO are we willing to go with here? lol | ||
302 | In Lua 5.1 and LuaJIT (?) metatables have no __type. | ||
303 | What about userdata? We could hijack the type() function, and dig deeper if it's a userdata, or even if it's a Thing? | ||
304 | |||
305 | |||
306 | Things as a metatable field - | ||
307 | In this plan test.__something is short for metatable(test).__something. | ||
308 | |||
309 | C can set metatables to non table values. NOTE - values, not variables, so likely useless. | ||
310 | |||
311 | __index is used to look up things that don't exist in a table. | ||
312 | If table isn't actually a table, then use __index as well. | ||
313 | Actually, in that case, the environment is the "table". | ||
314 | Either way, if we get to __index, and it's a function, call the function. | ||
315 | If __index is not a function, then use __index as a table to start the lookup all over again. | ||
316 | |||
317 | __newindex is similar, it's used if table.key is nil, AND __newindex exists. | ||
318 | Otherwise, it just sets table.key. | ||
319 | Again, if __newindow is a table, then start the lookup all over again with that. | ||
320 | |||
321 | metatable(module).__thing | ||
322 | metatable(module).__stuff | ||
323 | metatable(module).__values | ||
324 | |||
325 | __thing is a table that is a Thing. It can be just a reference to a __thing elsewhere. | ||
326 | So module is free to be full of random variables that are anything. | ||
327 | If module.foo is a table, it can have it's own metatable(foo).__thing. | ||
328 | A table with __thing will use Thing as a proxy table via __index and __newindex, as it does now. | ||
329 | |||
330 | moduleBegin(test, ...) -> thing stuff goes into test.__thing | ||
331 | thing(test, 'foo', 'string' ...) -> thing stuff gous into thing; setmetatable(test, Thing); test.__stuff[foo] = thing; test.__values[foo] = default | ||
332 | thing(test, 'foo', 'table' ...) -> thing stuff goes into thing; setmetatable(foo, Thing); test.__stuff[foo] = thing; test.__values[foo] = {}; test.foo.__thing = thing | ||
333 | thing(test.foo, 'bar', ...) -> setmetatable(foo, Thing); foo.__stuff[bar] = thing; foo.__values[bar] = default | ||
334 | copy(test, 'c' -> c.__thing = test.__thing;, c.__stuff = test.__stuff; copy test.__values to c.__values; setmetatable(c, Thing) | ||
335 | test.foo -> test.__index(test, 'foo') -> test.__values[foo]; if that's nil, and test.__stuff[foo].__thing, then return an empty table instead? | ||
336 | test.foo = v -> test.__newindex(test, 'foo', v) -> test.__values[foo] = v; test.__stuff[foo]:isValid(test) -> local value = test.__values[ self.names[1] ] | ||
337 | Which should call self.__stuff[*]:isValid(self) | ||
338 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) | ||
339 | test.foo(a) -> test.__index(test, 'foo') -> test.__values[foo](a) (but it's not a function) -> test.__values[foo].__call(test.__values[foo], a) | ||
340 | Doesn't seem useful. | ||
341 | If test.foo is a table, with a __thing then that might be -> test.foo.__call(test.foo, a) -> could do something useful with this. | ||
342 | get(test.foo, 'help') -> return test.foo.__thing[help] | ||
343 | skang.thing{test, 'foo', required=true} -> test.__stuff[foo].required = true | ||
344 | |||
260 | 345 | ||
261 | Stuff - | 346 | Stuff - |
262 | test{'foo', key='blah', field0='something', field1=1, ...} -> skang.things.foo.key='blah' skang.things.foo.field='something' ... | 347 | test{'foo', key='blah', field0='something', field1=1, ...} -> skang.things.foo.key='blah' skang.things.foo.field='something' ... |
@@ -511,6 +596,8 @@ end | |||
511 | 596 | ||
512 | skang.things.foo.value | 597 | skang.things.foo.value |
513 | skang.things.foo.pre_value | 598 | skang.things.foo.pre_value |
599 | |||
600 | |||
514 | ]] | 601 | ]] |
515 | new = function (module, pre) | 602 | new = function (module, pre) |
516 | local result = {} | 603 | local result = {} |