Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Other Programming > BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2
This is the forum to discuss the Wrox book Beginning Lua Programming by Kurt Jung, Aaron Brown; ISBN: 9780470069172
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 2nd, 2007, 10:07 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Confusion about what exactly is created in memory

Hi! I am unclear about a few things which I wondered if you could shed light upon:
1) In the MakeRing(Ring) function of pg 145. When the functions are being assigned to the Ring table:
for Name, Fnc in pairs(Methods) do
   Ring[Name] = Fnc
end
are complete copies of the functions being stored in the ring table, or simply pointers to the already existing functions in the Methods table?

2) In a similar vein, when functions are created as closures, is it a copy of the upvalue referred to in the external function that is created in the function, or simply a pointer to the upvalue?
E.g. using the example from the previous post:
local A = 10

local function Fnc(B)
  return A + B
end

print(Fnc(5))

when Fnc is created is a copy of the value of A (10) stored in Fnc or simply a pointer to A itself?

Regards, Ken

  #2 (permalink)  
Old June 2nd, 2007, 11:47 AM
Wrox Author
 
Join Date: Apr 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Ken,

Good questions!

> When the functions are being assigned to the Ring table:

> for Name, Fnc in pairs(Methods) do
> Ring[Name] = Fnc
> end

> are complete copies of the functions being stored in the ring
> table, or simply pointers to the already existing functions
> in the Methods table?

The pointer model is closer to the truth (the assignment is
"cheap", not "deep"), although see Rici Lake's excellent
posting to the Lua mailing list for a clearer and more accurate
way of looking at references in Lua:

  http://lua-users.org/lists/lua-l/2005-09/msg00056.html

> In a similar vein, when functions are created as closures, is
> it a copy of the upvalue referred to in the external function
> that is created in the function, or simply a pointer to the
> upvalue?

Again, the pointer model is more (but not completely) accurate.
By adding a couple lines to your example, you can see that it
is A, not the value A happens to be mapped to, that is captured
by Fnc as an upvalue.

  local A = 10

  local function Fnc(B)
    return A + B
  end

  print(Fnc(5)) --> 15

  A = 20

  print(Fnc(5)) --> 25

All the best,

Kurt
  #3 (permalink)  
Old June 2nd, 2007, 12:40 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the speedy reply Kurt. I shall read Rici's article with interest. Regards, Ken.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusion with PLATFORM, Help me Please! dpkbahuguna .NET Framework 2.0 10 September 28th, 2007 08:41 AM
foreach confusion stokerfed C# 2 June 13th, 2007 02:30 PM
Class' Confusion dpkbahuguna Beginning VB 6 4 September 20th, 2006 11:16 PM
Some confusion as to which SQL to use mark.uk SQL Server 2000 6 August 12th, 2004 10:47 AM
Stream Confusion benburrows General .NET 1 February 27th, 2004 12:04 PM



All times are GMT -4. The time now is 05:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.