Variables - jpbubble/NIL-isn-t-Lua GitHub Wiki
NIL requires all variables to be declared, before they can be assigned and used.
When a NIL script is read from a normal Lua Script, NIL will try to adopt all globals from the main Lua script by reading out _G. Not the most reliable method, but still. So you don't have to 'redeclare' them (they only should not contain 'nil' at the time or KABOOM! Of course variables with names that Lua doesn't see as a keyword, but NIL does will not work). Global variables declared in NIL will just be taken over by Lua just fine.
How to declare a variable?
All declarations require their own line in NIL (at least in the prototype version), and is very easy!
var a
var b
var c
a = "Jeroen"
b = "programmer"
c = 1975
print("My name is "..a.." and I'm a "..b..", born in "..v)
As you can see, pretty easy! NIL requires variable declaration in order to prevent bugs due to typos in your variable names, so it basically protects you.
Strongtyping?
Now "var" doesn't stand for "variable" in NIL, but for "variant", meaning they can contain any kind of value Lua supports.
Now this hasn't been fully worked out in the prototype version, but the plan is that future versions of NIL are more strict on this.
string a
string b
number c
a = "Jeroen"
b = "programmer"
c = 1975
print("My name is "..a.." and I'm a "..b..", born in "..v)
Now a and b should only contain a string, and c should only contain a number. Now you can alternatively also type "int" in stead of number, however that is only to avoid needless parse errors for C-coders who are too used of typing "int" for nummeric values, as non-integers will be allowed.
Alternatively this works too, however only with constants as defintions:
string a = "Jeroen"
string b = "programmer"
int c = 1975
print("My name is "..a.." and I'm a "..b..", born in "..v)
Also good to note is that when you type "string a" without a default value that a will automatically be an empty string and not a nil-value. "number b" or "int b" will automatically assign 0 to b when no default value's given. Booleans will without a value always be "false". Variables in the types "var", "table", "userdata" and "function" will by default be 'NIL' (and can in the prototype version not be assigned like strings, booleans and numbers in their declaration, like done above. Of course when it comes to functions see the section about them).
NIL has one type that Lua does not support. The "void" type, which is reserved for functions which are not allowed to return values. It cannot be used to declare variables.
Locals and Globals
In Lua all variables are global unless specifically declared as a local. In NIL it's the other way around. In NIL all variables are locals within the scope in which they are declared unless you declare them specifically as a global.
global string me="Jeroen" // This will be a global, and can be called anywhere regardles in which scope it was declared
string girl="Scyndi Scorpio" // Will be a local living in the scope where it's called.
Please note that this is not like php or Pyhton where globals must be redeclared inside every function or the function not picking them up. Globals can be declared and called anywhere you like (yes, even by Lua outside your NIL scripts). For cleanness sake however I STRONGLY recommend NOT to declare globals inside functions, but in the "ground scope" only.
Please note that if you do not add the keyword "global" prior to a declaration, even when in the ground scope, it is still a local. When you import a NIL-script as a module non-globals will then act as "private" variables as a result, as when you'd have added "local" to them in "pure Lua".
The reason I switched this rule around, is most of all because, globals are not that popular. Many programming teachers will tell you to avoid in all circumstances, unless there is a very good reason to use them (and some programmers claim those reasons do not exist). Therefore having globals by accident is basically not a good idea, and forcing you to declare them explicitly as such can be considered the safer route. Another thing is that NIL translate into pure Lua code, and once the translation is complete NIL won't be used for that specific script anymore and Lua rules only will apply from that point on, and in Lua locals are faster than globals for some odd reason. This was another reason, to make NIL put more emphasis on locals.