lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

foo.component.method(foo, ...) is support for this style of programming. If you really want it to look better you could wrap the methods in a function.
Simple example to illustrate my idea:

function redirect(method)
    return function(component, ...) return method(component.object, ...) end
-- declare this helper to register component actions
-- one could also just wrap the method directly i.e `redirect(function(object`
function component:action(name)
   self[name] = redirect(self[name])

function component:something()
   -- code here self == the object
component:action 'something' --register it as an action on the object the component is attatched to.
-- `foo.component:something()` will now work assuming components are wired up to have that .object reference.

While this example is clearly not complete, I hope it illustrates the suggestion clearly enough. I actually prefer the verbose method though, since a big part of composition based designs is being able to decouple the actions and the data (example is using an ECS) so having that clear reference too foo makes sense to me.


On Mon, Nov 26, 2018 at 10:30 AM Soni "They/Them" L. <> wrote:

On 2018-11-26 7:22 a.m., Pierre Chapuis wrote:
> On Mon, Nov 26, 2018, at 01:15, Soni They/Them L. wrote:
>> - Lua has little to no support for composition-based OOP, whereas it has
>> many features to support inheritance (__index/__newindex) and other
>> forms of OOP (self:methods()). This isn't a big deal, tho, as it can be
>> easily solved with a rather small patch.
> Can you give an example of what you would want here?
> Because for me, "composition-based OOP" does not need much
> language support. For what it's worth I almost never use inheritance
> in Lua (or any other language, really).

The classic Cratera[1] `foo:[component].method()` (note the colon) syntax.