I have a nested if in Lua. I have a variable inside the second if layer that I want to use in the first layer.
The variable is npcSpecimen.
if conditions then
local npcType = util.pickRandom(self.npcTypes)
local npcSpecimen = ""
if npcType == "spacebandit" then
local npcSpecimen = util.pickRandom(self.npcSpecies)
else
local npcSpecimen = util.pickRandom(self.npcSpeciesMutant)
end
local npcId = space.spawnNpc(spawnPosition, npcSpecimen, npcType)
end
If written this way, npcSpecimen will remain empty because the variable set within the if npcType remains only within that chunk. So to alleviate this, I could use global variable instead:
if npcType == "spacebandit" then
npcSpecimen = util.pickRandom(self.npcSpecies)
else
npcSpecimen = util.pickRandom(self.npcSpeciesMutant)
end
However according to the documentation, using global variable isn't the best practice and it's slower.
So what would be the best way to approach this so I could pass npcSpecimen to npcId?
Technically the answer is no, you can't use a local variable outside its scope, that's the whole point of local variables. However, you can just change the scope of the variable by declaring it outside of the block where you're using it:
local foo
if io.read() == "hello" then -- Just a dumb example condition :)
foo = "hello" -- This is not a global, as it was declared local above
end
print(foo)
However, note that the the following doesn't work, or, more precisely, doesn't do the same as the above:
local foo
if io.read()=="hello" then
local foo = "hello" -- This is another local
end
print(foo) -- This will *always* print nil
Related
General CS question because I was surprised by behavior of let in Netlogo.
If I declare a variable from within an if statement per below, is it common for the scope of that variable to be limited to that if statement?
I thought scope generally referred to functions rather than constructs like a loop or if statement. How common is that?
if x > y :
int i = 2
else:
int i = 3
print(i)
would return: "error: (i) does not exist"
I´m not sure, if this answers your question, but the Netlogo Programming Guide on local variables, created with let, states:
Local variables
A local variable is defined and used only in the context of a
particular procedure or part of a procedure. To create a local
variable, use the let command. If you use let at the top of a
procedure, the variable will exist throughout the procedure. If you
use it inside a set of square brackets, for example inside an “ask”,
then it will exist only inside those brackets.
to swap-colors [turtle1 turtle2]
let temp [color] of turtle1
ask turtle1 [ set color [color] of turtle2 ]
ask turtle2 [ set color temp ]
end
The same is true if a local variables is created within an if or ifelse statement. Therefore if you want to use the variable later on, than declare it before and outside the ifelse statement with let. Than assign the value with set within the ifelse statement.
I am attmepting to declare boost ptime at the global level. However I am encountering the following issue:
Before the main function I declare:
static boost::posix_time::ptime start_time;
Then inside the main function I initialize:
boost::posix_time::ptime start_time(boost::posix_time::microsec_clock::universal_time());
Then in a function outside the main function, I tried to convert to a string and print and I get "not-a-date-time";
Either the global variable is not being initialized or there is a scope problem?
Thank you for your help.
You define a local variable inside the main function, one that shadows the global variable but has nothing to do with the global variable.
You should not define the variable in the main function, only assign to it:
start_time = boost::posix_time::microsec_clock::universal_time();
boost::posix_time::ptime start_time(boost::posix_time::microsec_clock::universal_time());
Is not going to initialize start_time from the global scope. This is going to create a new variable named start_time that is scoped to main and hides the global start_time. When you try to use the global start_time in another function you get a not-a-date-time error because start_times was never initialized with an actual date.
You either need to do the initialization in the global space, do an assignment in main, or get rid of the global variable, declare it in main, and pass it by reference to the functions that need it. Personally I would go with the last option if possible as globals make the code harder to reason about.
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 6 years ago.
So I am learning the difference between global and local vars in Python 2.7 and as per my understanding
local var is one that is defined within a function and
globar var is one that is defined outside of the function.
I created this simple function to test how these local and global vars work when used in combination
def f():
global s
print s
s = "Python is great."
print s
Before I ran the function, I declared the global s
s = "I love python!"
f()
The output was:
>>> f()
I love python
Python is great
I understand till this part, but what I don't understand is, when I call the run a print s outside the function, why is it printing the local variable instead of the global one. Does this mean that the global var s is used once and discarded?
>>> print s
Python is great
can someone please explain this?
... what I don't understand is, when I call the run a print s outside the function, why is it printing the local variable instead of the global one.
There is no local s within the function. The global s statement causes the Python VM to use the s in the global scope even when binding it.
You declared s to be a global in the function, overriding the default behaviour, by using the global s statement. Because s is now a global, and you assigned a new value to it, that change is visible globally.
If I have the following code in a function, I will not get an error and I can compile no problem, however, once I put it in global scope I will get an error for "cannot allocate an array of size zero", along with several other errors. Why does this happen and how can I get rid of the errors. I am aware of the risk of global variables, this is just a simple test case.
int* intest[2];
intest[0] = new int;
You are allowed declarations in global scope but not allowed to use the new operator or the assignment. Thus you need the declaration int *intest[2] in global scope (and all your code would see it) but C++ requires the new to be in the sequence of your main code. (probably in some sort of start up function for the app).
EDIT: as pointed out by #phresnel you can use the new operator in this scope but not the assignment (this is unusual but not illegal). However the following new operators used as initiation will work for you:
int *x[2]={new int,new int};
In general the use of such a global buffer is highly discouraged and is considered an anti-pattern - if you can avoid using it you probably should.
int* intest[2];
Is valid placing in the local scope however :
intest[0] = new int;
is not.
The difference is that the upper one is a initalisation statement (creating the variable) and the lower one is a executed code segment.
Code that should be "executed" cant be called in the global scope, for example you cant call a function in the global scope. When would that function be called?
I can create how many variables i want in the global scope but i cant run code from it except from constructors being called when initialisating the global variables.
If you want to execute code such as :
intest[0] = new int;
You would have to execute it trough main or another function, otherwise the program would not know when to execute it.
AFAIK, the global scope only allow u put define and declaration on it. Whereas intest[0] = new int; is a assignment that c/c++ compiler shall fail while compile.
The scope evaluation order is well known/documented when using variables. However I can't find any information about the scope evaluation order when setting a variable.
One would presume it's the same list, but there appears to be a few caveats as demonstrated here:
<cfset qryChain = queryNew("id,next")>
<!--- add some data so the cfloop kicks in --->
<cfloop query="qryChain">
<cfset Next = StructNew()>
<cfset Next.id = qryChain.next>
</cfloop>
The above code is trying to reuse a variable name that it shouldn't, but fails in an unexpected way.
Since the cfsets are inside a query loop, item 4 of the scope evaluation order should be used for both. Instead Next is being evaluated as Variables.Next (item 6), and then Next.id is being evaluated as Variables.qryChain.next.id (item 4) and fails.
Is this documented anywhere? Is it simply items 1-6 of the "using" list above with a few caveats? Are these caveats intentional, or bugs? What other caveats are there?
I think I understand what is going on here. The behavior you are seeing is from scope searching when accessing variables in order to set them. When you set a variable without scoping it, ColdFusion will search the scopes to see if that variable exists anywhere first, and if it does, it will set it there.
In your first example:
<cfset qryChain = queryNew("id,next")>
<!--- add some data so the cfloop kicks in --->
<cfloop query="qryChain">
<cfset Next = StructNew()>
<cfset Next.id = qryChain.next>
</cfloop>
When you are creating your "Next" variable it is actually placing that variable into the VARIABLES scope, you can prove that if you dump the variables scope at any time during the looping process. You will see a "Next variable" with an empty struct.
The problem is in the next line. When you try to ACCESS the Next variable to set a new key into it, ColdFusion is finding the Next variable that exists in the Query result first, because while looping a query the Query scope (not really a scope, but it works like one in this case) has a higher precedence than the Variables scope. That variable does not contain a struct, so you will get an error about how you are referencing it.
Scope searching is happening, but it is not really while setting, it is while accessing in order to set.
Here is a working example to demonstrate this.
<cfset qryChain = queryNew("id,next")>
<cfset queryAddRow(qryChain, 3) />
<cfdump var="#qryChain#">
<!--- add some data so the cfloop kicks in --->
<cfloop query="qryChain">
<cfset Next = StructNew()>
<cfdump var="#variables#">
<cfset Next.id = qryChain.next>
<cfdump var="#qryChain#">
</cfloop>
in this example, I show that after you create the next variable it does exist in the variables scope, but when you immediately try to set a key into it without scoping that access you will instead get the NEXT variable from the current record in the query. This is only happening because the query happens to have record with a column name that happens to match the variable you are trying to use.
So why doesn't ColdFusion try to set the StructNew() into the Query scope (pseudo-scope)? The query cannot be manipulated using dot notation. Again, it is not really a scope. So in this sense it is read-only and is being skipped over
To manipulate a query resultset in CF you must use query functionsVARIABLES scope. Because unscoped variables are always placed in the VARIABLES scope. In the line where you try to set the id however, there is another phase going on in the background where it needs to access the variable in a read capacity first, and then try to perform the set. In this case, it DOES find the NEXT variable in the query because scope searching will occur first to determine in NEXT exists to set that key into and then when you try to set something to it, it fails.
As for your second set of examples, this is expected behavior and is easily explained.
In the first example, you are varing your variable (which places it in the local scope). You are then setting the value of that variable. When you set a value to a variable (without scoping it) ColdFusion checks to see if that variable already exists anywhere (so it does a scope search, again this is accessing at this point, not setting) it will find it in the local scope and then set the value there. After that you set the value again, this time properly scoping it, so no searching is done.
In the second example, you are not varing the variable when it is initially set, it does not exist anywhere, and so it is set to the variables scope. If it had already existed in the local scope, then ColdFusion would have found it and set it there (as in your first example), but since it did not exist, and it was not var'd it was set to the variables scope.
Finally, in your last example, you explicitly scope your variable and so it will be set in the local scope. You then set it again without scoping it. ColdFusion will find it in the local scope and overwrite it.
The moral of the story is, scope your variables. I tis important to get expected behavior. Scope-searching was never a good idea, but unfortunately, it is here to stay. I don't see anything here that I would call a bug, or even unpredictable behavior if you understand the way scope searching works.
Scope evaluation during assigment
I am aware of two different methods of scope evaluation when creating variables in ColdFusion. I haven't tested every possible instance, but this is how it should work.
The first instance uses the full list of scopes in evaluating unscoped variables. This is used by cfparam when creating variables. If ColdFusion does not find a variable with the given name then it will create it in the Variables scope.
The second instance uses the first 6 scopes in evaluating unscoped variables and then if unsuccessful will also create the variable in the Variables scope. This is employed by cfset and any other tags that creates variables, such as cfhttp with the result attribute, and cfsavecontents variable attribute.
As you observed there is the odd "sometimes ignore" the query scope issue. I would classify that as a bug, but someone may yet be able to provide a reason why the exception needs to be made.
Hoisting
Although ColdFusion was designed to copy JavaScript in a lot of ways (specifically cfscript) there is a subtle deviation which I haven't seen documented. With regard to functions (both script and tag), JavaScript employs hoisting, whereas ColdFusion does not.
Hoisting is the process of automatically moving the declaration of a variable to the top of the function, while maintaining the code placement of the assignment of the variable. The means that the scope of a variable will not change in JavaScript, but it can in ColdFusion.
Prior to CF9 the var keyword had to be used at the top of the function, essentially negating the need for hoisting. This is different from JavaScript, where var can be used anywhere in a function and employs hoisting. With CF9 ColdFusion adopted the declare anywhere philosophy, but neglected to implement hoisting.
In both the following examples JavaScript would only be dealing with a single scope, with x being function local.
<!--- sets variables in 1 scope --->
<cfscript>
var x = 0;
x = 1;
local.x = 7;
</cfscript>
Compared to:
<!--- sets variables in 2 scopes --->
<cfscript>
x = 1;
var x = 0;
local.x = 7;
</cfscript>
To avoid the potential pitfalls created by the lack of hoisting you can either var only at the top of the function, or do things as many have prior to CF9 and declare a var structure at the top of the function and prefix all variables with that (remembering to not name it local). e.g.
<cfset var localVars = StructNew()>
<cfset localVars.x = 7>
<cfset localVars.y = 1>
var vs local
In functions var appears to be a second class citizen to the local scope. If you try to set a local variable to the same name as an argument using var you will receive an error message saying Use local to define a local variable with same name. despite var and local supposedly being equivalent.
More
There may be additional caveats and bugs, however I'm not aware of there being any documented cases.
You're getting this error, presumably (since it's the error I received):
You have attempted to dereference a scalar variable of type class coldfusion.sql.QueryColumn as a structure with members.
This is because when you are within a cfloop for a query, the query scope takes precedence over the variables scope when evaluating a scope-less variable. However, when you are assigning scope-less variables (either within a cfloop query context or not) then you use the variables scope. So when you assign
<cfset Next = StructNew()>
You are setting the "Next" variable to the variables. scope. However, when you attempt to evaluate Next (by referring to its key, .id), like so:
<cfset Next.id = qryChain.next>
Now you are getting Next from the query scope (and since Next in the query scope is not a structure, you get an error).
So, to try to answer your main question - when you are assigning a variable with no scope specified, CF will put the variable in the variables scope. When you attempt to evaluate a variable with no scope specified, then it will use the normal scope-precedence rules to find a match (in this case, finding one in the query scope).