lua-users home
lua-l archive

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

On Tuesday, September 24, 2013, Andrew Starks wrote:

On Tuesday, September 24, 2013, Dirk Laurie wrote:
2013/9/23 Andrew Starks <>:
> I have many setter methods that handle properties that represent an
> inventory of one or more objects of the same class (to borrow from
> OOP).
> So that I don't need to write a "set_prop" and "set_props" function, I
> write this quite a bit:
> local function f(arg, ...)
>     if (...) then
>         return arg, f(...)
>     else
>         return arg
>     end
> end

I assume something non-trivial is being done too, i.e

local function f(arg, ...)
    if (...) then
        return arg, f(...)
        arg = complicated_function_of(arg)
        return arg
    end end

Not really and it wouldn't be the first time I've labored under an ignorance that others shed in chapter 1 of PiL. :P This example might be better:

function f:add_one_or_more_props(args,...)
--do stuff to add props 

if (...) then
  return self

Or another variant: 

function f:add_one_or_more_props(args,...)
  if not args then return self end
  --do stuff

I think that it's a much simpler question than I communicated and the second version might be my answer, although erroring on the first call, if it doesn't contain arguments, is not possible...


I shouldn't post when I first wake up...

This question only makes sense in the cases where I'm returning the values of the recursion as independent values, effectively creating:

return return_val_from_call1, return_val_from_call2, return_val_from_call3

By something like:

return return_val_from_call, f(...)

It's not a problem now, in that I can do it. I'm just looking for a shorter way.