lua-users home
lua-l archive

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


Thanks Geoff, the solution you suggested worked for me.

class.A()
function A:fun1(val)
    print(val)
end
function A:fun2()
    --return self.fun1;
    return function(...)
        return A:fun1(...)
    end
end
Aobj = A()
fptr = Aobj.fun2()
fptr('man');



Thanks & Regards,
Muqtheear.S


On Wed, May 22, 2013 at 4:51 AM, Geoff Leyland <geoff_leyland@fastmail.fm> wrote:
On 22/05/2013, at 11:13 AM, Muqtheear S <muqtheear.s@gmail.com> wrote:

> Hi all,
>         When I execute below code, fun1 is getting called, but val is always nil(supposed to be 'man'). Please suggest me
>
> class.A()
> function A:fun1(val)
>     print(val)
> end
> function A:fun2()
>     return self.fun1;
> end
> Aobj = A()--creating an object
> fptr = Aobj:fun2()
> fptr('man');

You need to pass the Aobj to fptr.  The last line should be: fptr(Aobj, 'man').

Alternatively, fun2 could be

function A:fun2()
    return function(...) return self:fun1(...) end
end

(Since you're using some class library thing, I haven't tested this code)