lua-users home
lua-l archive

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


Yes, that's exactly what I understood: you have a variable/conditional "path" to the property of object "a" that interest you, and then "d()" is a method on that property that must be used where you want to it to act on the object "a".

My approach in pure Lua, using a closure to enclose the value of "a", plus a function in Lua to build an intermediate object should work for your case even if you modified it:

    a:[test and 'b' or 'c'].d("additional parameters:", {12, 34}, 'sample') 

which is also writable in Lua as:

    (function(a)
        local __o__ = a;
        return function(...)
            return  (__o__[test and 'b' or 'c']).d(__o__, ...)
        end
    end)(d,"additional parameters:", {12, 34}, 'sample')
 
See how I added also the other parameters (which are passed to the internal function here using a vararg, but the vararg is not mandatory).

The typical usage would be to create dynamic properties in a OOP language with facets:

    value = (some _expression_ selecting an object):[some _expression_ computing a property name].get();

    (some _expression_ selecting an object):[some _expression_ selecting a property name in the object].set(value);

which can be written as:

   value = (function(o)
        local __o___ =  o;
        return function() return  __o__.get(__o__[some _expression_ selecting a property name in the object]) end
    end)(get, some _expression_ selecting an object);

   (function(o, v)
        local __o___ =  o;
        return function() return  __o__.set(__o__[some _expression_ selecting a property name in the object]) end
    end)(some _expression_ selecting an object, value);

There's many variants possible depending on which part is variable or not or dependant on the object that the "apparently simple" syntax
    a:b.c(d)
does not disambiguate clearly. And this would be even worse if we allowed this:
    a:b:c.d(e)
which could be understood differently as any one of:
    ((a:b):c).d(e)
    (a:(b:c)).d(e)
    a:((b:c).d)(e)
depending on associativity, and which object must be passed to the final dynamic method named "d" here.


Le ven. 30 nov. 2018 à 02:56, Coda Highland <chighland@gmail.com> a écrit :
On Thu, Nov 29, 2018 at 11:08 AM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
>  local a = a
>  local x = test and a:b or a:c
>  x:d()

I mean...

a:[test and 'b' or 'c'].d()

/s/ Adam