Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

What is the maximum lua can take
Started by JoePringles Jun 21 2018 10:46 AM

- - - - -
30 replies to this topic

[TOPIC CONTROLS]
Page 2 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#26

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 814 posts
  • Corona SDK

Perhaps also worth mentioning is that you can use do blocks to limit the lifetime of locals:

-- stuff

do
  local a, b, c

  -- do stuff with a, b, c
end

-- left scope, so slots for a, b, c up for grabs

but I wouldn't go too crazy with this.



[TOPIC: post.html]
#27

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

"in this scope" (ie, the scope of function m.doit1) there are no locals (nor upvalues)

at this point, at module scope, there are still only three locals yet defined

 

Are you saying the locals from the file level scope are not visible or that they don't count against your maximum limit within the scope of the function?

 

Are you saying I could have 200 locals in the file level scope and an extra 200 in the function?

 

In truth, I have always been a little unclear on the  upvalue concept.  Looks like I need to do more reading too.



[TOPIC: post.html]
#28

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

@davebollinger,

 

So, locals at the file/module level are 'upvalues' (external locals) in the scope of the function.

 

 

Thanks!  I learned something new too!  Also, very elegant example now that I know what is going on.

 

I'll update my post in a moment to properly reflect the local, upvalue, etc. terms.



[TOPIC: post.html]
#29

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 posts
  • Enterprise

Are you saying the locals from the file level scope are not visible or that they don't count against your maximum limit within the scope of the function?

 

Are you saying I could have 200 locals in the file level scope and an extra 200 in the function?

 

they're visible, yes, but they're never referenced, so they're not upvalues of the function, so don't count against that limit (and they're certainly not locals of the function, so don't count against that limit either)

 

yes - you can have 200 at module level and another 200 at function level



[TOPIC: post.html]
#30

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

OK, I updated the example and I believe I have it right this time.

 

Thanks again @davebollinger!



[TOPIC: post.html]
#31

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 posts
  • Enterprise

fwiw, for those whose eyes gloss over when they start seeing words like "closure", "environment", "upvalue", etc..

 

an upvalue is a variable that has been bound to the environment of a function.

 

great, but what the heck does THAT mean??  :D  ie, the answer is more confusing than the question, right?

 

functions in Lua are first-class citizens, and may "travel" outside of the scope in which they were defined.  in order to continue working outside of their defining scope, they need to retain their original environment.  this combination of a function with its environment is called a closure.  all functions in Lua are closures, though they may not necessarily need/use their environment, because they may not actually reference any variables outside of their local scope.  any variables from outer scopes that ARE referenced and DO need to be bound into the closure environment are called upvalues.

 

example:  ponder the following code, and once you "get" how 'n' is "remembered" then you'll have a workable understanding

print("at this point 'n' does not exist", n)

local function makeSquaringFunction(n)
  print("at this point 'n' is a local of function makeSquaringFunction", n)
  return function()
   print("at this point 'n' is an upvalue of the returned squaring function", n)
   return n*n
 end
end

print("at this point 'n' does not exist (again)", n)

local threeSquared = makeSquaringFunction(3)

print("at this point 'n' does not exist (still)", n)

print("three squared = ", threeSquared()) -- WHAT?!?!
-- ie, how can it possibly still "know" that n = 3?!?!  that seems like "voodoo"!!!

hth




[topic_controls]
Page 2 of 2 1 2
 
[/topic_controls]