Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Question about the useage of the local function in module
Question about the useage of the local function in module
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Yeedoo
China (12 posts) Bio
|
Date
| Tue 21 Feb 2012 05:14 AM (UTC) Amended on Tue 21 Feb 2012 05:15 AM (UTC) by Yeedoo
|
Message
| In a module or package. What's the different with the function and the local function?
Just the local function cant be seen from outside the module?
I tried to setup a module with a local function
wang_test={}
function wang_test.foo1(str)
local _f=foo2 ()
_f()
end --function wang_test.foo1()
---------------------------------
local function foo2 ()
return function ()
print('This is my test')
end --
end --local function f_2
but When i use the module, i got that
attempt to call global 'foo2' (a nil value)
What's the problem? Thank you! | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #1 on Tue 21 Feb 2012 05:16 AM (UTC) |
Message
|
Yeedoo said: Just the local function cant be seen from outside the module?
That's all it does, yes. The function is assigned to a new local variable instead of the globals list. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Yeedoo
China (12 posts) Bio
|
Date
| Reply #2 on Tue 21 Feb 2012 05:42 AM (UTC) |
Message
|
Twisol said:
The function is assigned to a new local variable instead of the globals list.
But the local function foo2 was called only in my module.
In the mushclient world,i just write thus
require'wang_test'
wang_test.foo1()
| Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #3 on Tue 21 Feb 2012 06:18 AM (UTC) Amended on Tue 21 Feb 2012 06:19 AM (UTC) by Twisol
|
Message
| Oh, I see. I thought those were in separate files for some reason.
You can't use a local function (or a local anything, really) before it's been defined. A local can only be accessed from the point it's created up until its scope closes. A global can be used from anywhere, so long as it's been created first.
All you need to do is re-order your function definitions, or else pre-create the locals.
wang_test={}
local function foo2 ()
return function ()
print('This is my test')
end --
end --local function f_2
function wang_test.foo1(str)
local _f=foo2 ()
_f()
end --function wang_test.foo1()
|
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Yeedoo
China (12 posts) Bio
|
Date
| Reply #4 on Tue 21 Feb 2012 06:46 AM (UTC) |
Message
|
Twisol said:
Oh, I see. I thought those were in separate files for some reason.
You can't use a local function (or a local anything, really) before it's been defined. A local can only be accessed from the point it's created up until its scope closes. A global can be used from anywhere, so long as it's been created first.
All you need to do is re-order your function definitions, or else pre-create the locals.
I got that! Thank you for so quick reply | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
17,372 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top