Tip: Function at line has more than 60 upvalues

Started by Zefk, September 13, 2016, 03:13:13 AM

Previous topic - Next topic
September 13, 2016, 03:13:13 AM Last Edit: October 05, 2016, 05:11:34 AM by Zefk
I had more than 60 upvalues and got an error. A lua limit. Upvalues are variables declared outside of a particular function. I solved it with a table. This solved the 200 Local Variables Limit Problem as well.

Example:
https://forums.coronalabs.com/topic/6648-function-at-line-has-more-than-60-upvalues/?p=187272

Declaring over 60 arrays gives an over 60 upvalue error. Put the arrays in a table.

local yellow ={
      num1_place ={}
      num2_place ={}
      etc
}

yellow.num1_place[1] = 0
yellow.num1_place[2] = 0
etc

yellow.num2_place[1] = 0
yellow.num2_place[2] = 0
etc


Other interesting solutions:
I have not tested them, but go for it.
https://forums.coronalabs.com/topic/19555-solved-200-local-variables-limit-problem/ (Function table)
https://ask.wireshark.org/questions/46598/lua-60-upvalues-limit-in-function-solved (array)

Why would you ever need a function with 60 upvalues???

September 13, 2016, 09:15:14 AM #2 Last Edit: September 13, 2016, 09:19:00 AM by Zefk
1. The person might want to go crazyyyyyyy
2. The programmer decides to not use coordinates for numbered png images. That requires more variables usage.
3. The programmer wants a picture based ticking clock. Each tick hand is a different design. Not just color change or direction rotation for a single image.

In your examples there are repetitive values so you should use an array anyway.

September 13, 2016, 04:26:56 PM #4 Last Edit: September 15, 2016, 02:51:38 PM by MetalZelda
Something like this is more convenient, faster, and cleaner

Code (lua) Select
local vars = {}
local max_vars = 61 -- Maximum variable, minimum is 1, maximum is infinite

-- This is done when the script is created, it does the same thing as the example
-- Except that instead of vars.var1, it is vars[1]

for i = 1, max_vars do
  vars[i] = 0
end

local function Update(event)
  for i = 1, #vars do
    print(vars)
  end
end

return vars


Declaring these thing 1 by one is a waste of time
But yeah, 60 upvalues is completely crazy

I can declared a thousand in a few minutes with copy, paste, search, and replace. Not much time in my opinion, but I do not see me ever needing that much. It all depends on how one wants to design their code.


I agree with the bear.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."


September 15, 2016, 01:44:20 PM #9 Last Edit: September 15, 2016, 01:51:37 PM by MetalZelda
lèl

I did not really understand what OP want until now :P

Quote from: Zefk on September 13, 2016, 05:26:18 PM
I can declared a thousand in a few minutes with copy, paste, search, and replace. Not much time in my opinion, but I do not see me ever needing that much. It all depends on how one wants to design their code.



There are faster solutions, the one you are saying is the most time wasting



There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P

That is what comments are for and I name my variables in a way that I can find them and modify all of them in seconds. Code is easy to follow for those with lots of practice. Plus gedit and most text editors have search options for people that have trouble with finding a variable. Never happened to me though.  I do not remember the last time I had trouble finding a variable.

September 15, 2016, 10:27:43 PM #14 Last Edit: September 15, 2016, 10:31:13 PM by zutokaza
Quote from: Christopho on September 15, 2016, 02:22:11 PM
He already did above, it takes 10 lines :)

He meant to name 'more' solutions if he wanted.

Quote from: MetalZelda on September 15, 2016, 02:32:36 PM
There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P

Your solution is good, but like Zefk I like to give my variables good names. I would not use an array for good variable naming. I also like to see my variable list and not a few listed arrays for name changes. Your solution depends on many factors or whether the variables are related to each other. The table solution is flexible. Especially, when you are designing your code. I will not know how many variables I will end up with in a complex project.
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616