How to access a function inside a function in extern module?

I thought this one is an easy one, but now I’m trying to access this for about an hour and can’t figure it out.

Here is my problem:

I have an external module, like this one…

local M = {} local innerfunction -- functions are now local: local testFunction1 = function()     print( "Test 1" )     -- now the function I want to access later     innerfunction = function (event)        -- my inner function code     end -- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction -- Finally, return the table to be used locally elsewhere return M

Now I want to access not only testFunction1 which I can easily by doing this…

local example = require "examplemodule" example.testFunction1() -- prints "Test 1" to terminal

Instead I want to access innerfunction directly, like for example with a Runtime listener in the main code.

How can this be done?

My first guess was to do this:

local example = require "examplemodule" Runtime:addEventListener ("touch",example.innerfunction)

But with this code I get an error.

Is there a way to access this kind of ‘nested’ functions in a module?

Any help welcome!

Best,

Daniela

Daniela,

Hi.  You’re confusing the concept of references and pointers.  Lua does not have a pointer variable types.  All variables contain either scalar values or references to other objects (tables).

At this point in your code, 

-- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction

The variable (from higher in the code) 

local innerfunction

 contains ‘nil’.  So, when the module is loaded, you are assigning ‘nil’ to ‘M.innerfunction’

Later, when you run the function ‘testFunction1()’ the variable ‘innerfunction’ is assigned a new value.  This however has no affect on ‘M.innerfunction’.

It is also very important to understand, that EVERY time you run ‘testFunction1()’, a new function is created and assigned to ‘innerfunction’, which is pretty wasteful.

I’m not sure why you’re trying this, regardless, hopefully the following code will help you understand this concept:

-- Place this code in daniela.lua local m = {} local innerFunc local count = 0 local outerFunc = function() count = count + 1 print("\n--------------------") print( "outerFunc called " .. count .. "times") print( "BEFORE: innerFunc == " .. tostring( innerFunc ) ) -- Creates NEW copy of function every time. m.innerFunc = function() print("Do something") end print( "AFTER: innerFunc == " .. tostring( innerFunc ) ) end m.outerFunc = outerFunc return m

Now test the above module:

local daniela = require "daniela" print(daniela.outerFunc) print(daniela.innerFunc) daniela.outerFunc() daniela.outerFunc() print(daniela.outerFunc) print(daniela.innerFunc)

Question: Are you trying to achieve an OOP like hierarchy, including private and public data?

Thank you (again) for your fast help! Now things aren’t so confusing anymore :slight_smile:

I shouldn’t do so much ‘learning by doing’ with lua I guess. Maybe I should continue reading the language handbooks. :wink:

Daniela

Daniela,

Hi.  You’re confusing the concept of references and pointers.  Lua does not have a pointer variable types.  All variables contain either scalar values or references to other objects (tables).

At this point in your code, 

-- assign a reference to the above local function M.testFunction1 = testFunction1 M.innerfunction = innerfunction

The variable (from higher in the code) 

local innerfunction

 contains ‘nil’.  So, when the module is loaded, you are assigning ‘nil’ to ‘M.innerfunction’

Later, when you run the function ‘testFunction1()’ the variable ‘innerfunction’ is assigned a new value.  This however has no affect on ‘M.innerfunction’.

It is also very important to understand, that EVERY time you run ‘testFunction1()’, a new function is created and assigned to ‘innerfunction’, which is pretty wasteful.

I’m not sure why you’re trying this, regardless, hopefully the following code will help you understand this concept:

-- Place this code in daniela.lua local m = {} local innerFunc local count = 0 local outerFunc = function() count = count + 1 print("\n--------------------") print( "outerFunc called " .. count .. "times") print( "BEFORE: innerFunc == " .. tostring( innerFunc ) ) -- Creates NEW copy of function every time. m.innerFunc = function() print("Do something") end print( "AFTER: innerFunc == " .. tostring( innerFunc ) ) end m.outerFunc = outerFunc return m

Now test the above module:

local daniela = require "daniela" print(daniela.outerFunc) print(daniela.innerFunc) daniela.outerFunc() daniela.outerFunc() print(daniela.outerFunc) print(daniela.innerFunc)

Question: Are you trying to achieve an OOP like hierarchy, including private and public data?

Thank you (again) for your fast help! Now things aren’t so confusing anymore :slight_smile:

I shouldn’t do so much ‘learning by doing’ with lua I guess. Maybe I should continue reading the language handbooks. :wink:

Daniela