[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Question about the useage of the local function in module

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Question about the useage of the local function in module
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Yeedoo   China  (12 posts)  [Biography] bio
Date Tue 21 Feb 2012 06:46 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Tue 21 Feb 2012 06:18 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Yeedoo   China  (12 posts)  [Biography] bio
Date Tue 21 Feb 2012 05:42 AM (UTC)  quote  ]
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()

[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Tue 21 Feb 2012 05:16 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Yeedoo   China  (12 posts)  [Biography] bio
Date Tue 21 Feb 2012 05:14 AM (UTC)  quote  ]

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!
[Go to top] 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.


928 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]