Scoping associates a name with an entity. For an object to be *in scope* means that it is possible to write a snippet of code referencing value and location of that object by its name. Lexical scoping refers to a proportion of text whereas dynamic scoping corresponds to the proportion of run time.
Questions tagged [scoping]
584 questions
11
votes
1 answer
What magic in "our" or "use vars" satisfies "use strict qw(vars)"?
I have working code, but I am trying to understand why it works. I am also trying to learn more about the internals of Perl 5 (perlbrew, perl-5.26.1, Cygwin x64).
I know from perlvar and strict that use strict 'vars' works by setting flags in $^H. …

cxw
- 16,685
- 2
- 45
- 81
11
votes
1 answer
Is scoping broken in VBA?
Say you have this code in a module called Module1:
Option Explicit
Private Type TSomething
Foo As Integer
Bar As Integer
End Type
Public Something As TSomething
In equivalent C# code if you made the Something field public, the code would…

Mathieu Guindon
- 69,817
- 8
- 107
- 235
11
votes
1 answer
Referring to a class defined inside a function scope
In C++1y, it is possible for a function's return type to involve locally defined types:
auto foo(void) {
class C {};
return C();
}
The class name C is not in scope outside the body of foo, so you can create class instances but not specify their…

Heatsink
- 7,721
- 1
- 25
- 36
10
votes
1 answer
In Julia, why can "if false" statements in local scope modify function definitions?
In Julia 1.4.1, if I define a function in a global scope, modifications after "if false" statements do not affect it, as expected:
test()=0
if false
test()=1
end
println(test())
This prints "0", as it should. However, when I enclose this code…

L. Mann
- 143
- 6
9
votes
3 answers
javascript: how to refer to an anonymous function within the function itself?
if arguments.callee is not allowed in "use strict", and we can't do
var f = function g() {
//g
}
because in IE that wouldn't work (or that would work "weirdly") http://kangax.github.com/nfe/#jscript-bugs, then what other options do we have to…

Pacerier
- 86,231
- 106
- 366
- 634
9
votes
3 answers
JavaScript local scoping: var vs. this
I can't seem to get my head around a specific case of scoping for JavaScript variables. Different from other examples and questions I have found, I am interested in the scoping for nested functions.
I've set up an example at this JSFiddle. The…

Alpha
- 7,586
- 8
- 59
- 92
8
votes
5 answers
python noobie scoping question
I wrote this code:
x = 0
def counter():
x = 1
def temp(self):
print x
x += 1
return temp
Trying to test if python is lexical or dynamic scope. My thinking was that
y = counter()
y()
Should either print 0 or 1, and that would tell me how…

amatsukawa
- 841
- 2
- 10
- 21
8
votes
1 answer
Scoping and evaluating functions in R
Given the following function
f <- function(x) {
g <- function(y) {
y + z
}
z <- 4
x + g(x)
}
If one runs the following code in R why is the answer 10? I'm a little confused about how y plays into this question.
z <-…

nzhanggh
- 121
- 6
8
votes
0 answers
Primitive function in R
Can someone give me explanation for the below sentence highlighted in Bold.
"Primitive functions are only found in the base package, and since they operate at a low level, they can be more efficient (primitive replacement functions don’t have to…

Naveen Gabriel
- 679
- 2
- 9
- 25
8
votes
0 answers
Erroneous code diagnostics report in RStudio when sourcing functions via source
I'm working in RStudio on a simple analysis where I source some files via the source command. For example, I have this file with some simple analysis:
analysis.R
# Settings…

Konrad
- 17,740
- 16
- 106
- 167
8
votes
1 answer
Environments in R Shiny
At http://shiny.rstudio.com/articles/scoping.html the rules for scoping in shiny are well explained. There are 3 environments or levels nested inside each other: objects available within a function, within a session and within all sessions. Using <-…

steinbock
- 726
- 1
- 12
- 25
8
votes
2 answers
Accessing Rails Controller instance variables in CSS
So I've seen lots of discussion on SO about using erb in your CSS files. I can get ERB to process the CSS files by using the <%= %> syntax and adding .erb to the file, but what I really need is to access instance variables in the…

rurabe
- 447
- 4
- 13
7
votes
3 answers
Javascript: z = z || [] throws an error when not using VAR - why?
Out of just intellectual curiosity, why does javascript accept
var z = z || [];
to initialize z (as z may defined initially)
but without var, it throws an error (in global space)
z = z || [];
(if z is previously undefined)
In the global space you…

ck_
- 3,353
- 5
- 31
- 33
7
votes
1 answer
Symbols Created in Stash at Runtime Not Available in PseudoStash in Raku
This question started with me trying to figure out why symbols created at runtime are not available to EVAL.
outer-EVAL.raku
#!/usr/bin/env raku
use MONKEY-SEE-NO-EVAL;
package Foobar {
our $foo = 'foo';
our sub eval {
say OUTER::;
…

JustThisGuy
- 1,109
- 5
- 10
7
votes
1 answer
Eagerloading with scoping in rails3
I have been trying to eager load associations based on some scope in my rails3 app, but could not find any solution.
My app has following models:
class Project
has_many :entries
has_many :to_dos
class ToDo
has_may :entries
has_many :tasks
…

jayandra
- 296
- 2
- 8