This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Agoldfish's profile picture

Help With Calculator?

Started by Agoldfish, 17 January 2014 - 12:54 PM
Agoldfish #1
Posted 17 January 2014 - 01:54 PM
Hi, I was making a calculator, and I came along some errors, all of the "operator functions" are nil, although they have information in them! I tried respelling all of them, but to no avail. Help!
The code can be found here.

Thanks for any help.
OReezy #2
Posted 17 January 2014 - 03:05 PM
I think the issue here is you are calling on those functions before they are defined, so it gets a nil. This guy explains it better than I can

Spoiler
Indeed, but if your read the program, you would see that the variables are declared in the main block of the program, same as the functions which used them. Ergo it should have worked.

Well i think the matter lies with the order of declaration - for example:
If you do this:

local function f()
  print(x)
end
f()
x = 6
f()

It will print "5 \n 6"
but if you do


local function f()
  print(x)
end
local x = 5
f()
x = 6
f()

it will print "nil \n nil"

because it compiles the code from top to bottom, it sets all not defined variables environments to global - in this case, there doesnt exist any global x! so it return Nil.
To understand that better: this is the way the compiler works:

local x = 5  -- Declare variable x as local -> create new Environment _ENV
local function f()
  print(x) -- x translates to _ENV["x"], because it was declared physically beforehead
end

vs

local function f()
  print(x) -- "Was it declared beforehand?" -> No -> x translates to _G["x"]
end
local x = 5 -- -- Declare variable x as local -> create new Environment _ENV
(note that there is no actual "current" Environment, it rather accesses the register directly, but for simplicities sake i left it at that)

now you have 2 totally different Environment references:
In the first case, the variable X in the function has the Environment _ENV.
In the second case, the variable X has the global Environment _G

So if you would declare a variable "local chestcokecount" before you declare the function it would be fine. (you can just declare without a value at all)


To give another example:

local function f()
  print(x)
end
local x = 8
_G["x"] = 5
f() --> 5
x = 6
print("Local:"..x)
f() --> 6
returns this:

5
Local:6
5
Edited on 17 January 2014 - 02:26 PM
Lyqyd #3
Posted 17 January 2014 - 03:27 PM
Yes, you need to declare your local functions before the code that should call them.
Agoldfish #4
Posted 17 January 2014 - 04:16 PM
Yes, you need to declare your local functions before the code that should call them.
I think the issue here is you are calling on those functions before they are defined, so it gets a nil. This guy explains it better than I can

Spoiler
Indeed, but if your read the program, you would see that the variables are declared in the main block of the program, same as the functions which used them. Ergo it should have worked.

Well i think the matter lies with the order of declaration - for example:
If you do this:

local function f()
  print(x)
end
f()
x = 6
f()

It will print "5 \n 6"
but if you do


local function f()
  print(x)
end
local x = 5
f()
x = 6
f()

it will print "nil \n nil"

because it compiles the code from top to bottom, it sets all not defined variables environments to global - in this case, there doesnt exist any global x! so it return Nil.
To understand that better: this is the way the compiler works:

local x = 5  -- Declare variable x as local -> create new Environment _ENV
local function f()
  print(x) -- x translates to _ENV["x"], because it was declared physically beforehead
end

vs

local function f()
  print(x) -- "Was it declared beforehand?" -> No -> x translates to _G["x"]
end
local x = 5 -- -- Declare variable x as local -> create new Environment _ENV
(note that there is no actual "current" Environment, it rather accesses the register directly, but for simplicities sake i left it at that)

now you have 2 totally different Environment references:
In the first case, the variable X in the function has the Environment _ENV.
In the second case, the variable X has the global Environment _G

So if you would declare a variable "local chestcokecount" before you declare the function it would be fine. (you can just declare without a value at all)


To give another example:

local function f()
  print(x)
end
local x = 8
_G["x"] = 5
f() --> 5
x = 6
print("Local:"..x)
f() --> 6
returns this:

5
Local:6
5
Thank you. Time to do some major organization. :/