Is there a way to define constants in Corona / Lua? [import]uid: 295 topic_id: 1384 reply_id: 301384[/import]
What type of constraints are you referring to? [import]uid: 7366 topic_id: 1384 reply_id: 3834[/import]
There are no constants in lua. [import]uid: 5712 topic_id: 1384 reply_id: 3835[/import]
I want to define something like constants that are in one header file so that I can use the constants instead of magic numbers in my code. And I don’t want’ to use global variables as constants. Any way I found a way to do it on the Lua IRC.
[code]
–[[
TOTconstants – for simulating constants
To access these constants in other files require this module and
assign it to a local variable named ‘CON’ then use CON.ST.theValue
–]]
module(…, package.seeall)
tbl = {}
– TOTbuttonHandlers constants
tbl.stagingX = 16 – Selected piece appears here for rotation to be used
tbl.stagingY = 450
tbl.currentPlayer = 1
tbl.selectedButton = 1 – Default starting button is always type 1
tbl.buttonRotation = 0 – and it’s rotation is 0
tbl.bdXOffset = 16 – offsets of board on plaffield
tbl.bdYOffset = 96
tbl.bdPosSize = 24 – size of one position on board
– TOTxyToBoardXY constants
tbl.hMargin = 30 – used in TOTxyToBoardXY
tbl.vMargin = 30 – used in TOTxyToBoardXY
– TOTsetupField constants
tbl.playfieldY = 240
tbl.typeButton1X = 30; tbl.typeButton1Y = 450
tbl.typeButton2X = 60; tbl.typeButton2Y = 450
tbl.typeButton4X = 90; tbl.typeButton4Y = 450
tbl.typeButton8X = 120; tbl.typeButton8Y = 450
tbl.rotationButtonCClockwiseX = 220; tbl.rotationButtonCClockwiseY = 450
tbl.rotationButtonClockwiseX = 250; tbl.rotationButtonClockwiseY = 450
function readOnly(t)
local proxy = {}
local mt = { – create metatable
__index = t,
__newindex = function (t,k,v)
print(“ERROR: attempt to update a read-only table”)
end
}
setmetatable(proxy, mt)
return proxy
end
ST = readOnly(tbl) – to access constants in other files use CON.ST.theValue
[/code] [import]uid: 295 topic_id: 1384 reply_id: 3837[/import]
Very nice, thanks for sharing
Carlos [import]uid: 24 topic_id: 1384 reply_id: 4099[/import]
Hi @all…
I felt inspired and wanted to have a simple and powerful way to declare constants without writing them down inside a special module but “anywhere” in my code. Here is the result:
http://developer.anscamobile.com/code/universal-constants-module-very-easy-usage
[import]uid: 6928 topic_id: 1384 reply_id: 4122[/import]
Thanks.
But isn’t it better programming practice to have all constants is one place? That’s how we used to do it 25 years ago. [import]uid: 295 topic_id: 1384 reply_id: 4141[/import]
25 years ago I had all code (or even hex-data) in one file anyway 
I like to have constants in the head of my “main” code. Which would be main.lua … and not in a separate file.
But even if you want it in an extra file. I believe in abstraction and would prefer to require my Const Module in that file than to mix implementation with the usage of the module.
But that is academical and there is no “style” I have to follow…
I program for 28 years now and to be honest… the only reason why I think constants are “cool” is for helping the compiler/interpreter to optimize code.
I think I never ever thought about them as “make sure they are not changed”. Cause I just don’t change them 
My implementation was more for having fun with Lua and try to get them behave and look like I think “constants” should behave “assign and stay unchanged forever” …
I am bored sometimes
[import]uid: 6928 topic_id: 1384 reply_id: 4160[/import]
During development I like to keep my constants in one central file so I know where to find the constants which I constantly tweek.
And, your implementation IS very useful AND instructive for me. [import]uid: 295 topic_id: 1384 reply_id: 4163[/import]
Is there no way to define constants without changing (adding to) the compiled code?
I’d like to use contants to make my code easier to read and quicker to adjust (by tweaking constant values). But I don’t want to do this if it makes my compiled code bigger (and slower?) [import]uid: 34083 topic_id: 1384 reply_id: 24767[/import]