Why does redeclaring output inside this if-statement not generate an error?
let output = 0
if counter < message.count {
let output = counter //This should throw an error, right?
counter += 1
} ...
The scope inside the if-statement knows about output as proven here, when trying to change the value of output instead of re-declaring it:
let output = 0
if counter < message.count {
output = counter //ERROR: Cannot assign to value: 'output' is a 'let' constant
counter += 1
} ...
There is no error because it is perfectly legal to declare a variable inside a closure with the same name of a variable which has been declared outside of the closure. It shadows the "outside declared variable".
In case your sample code is inside a class you could still access the "outside declared variable" using self:
class Foo {
let output = 0
func baa() {
let output = 1
print(output)
print(self.output)
}
}
Using this:
let foo = Foo()
foo.baa()
prints:
1
0
let output = counter is declaring a new variable (output) in the scope of the if statement, which has nothing to do with the output variable declared outside.
Edit
The code extract below illustrates that the output variables are not the same, despite their name. The value that gets changed is the one of the local output variable, not the one outside.
var message = [String]()
let output = 2
var counter = -1
if counter < message.count {
let output = counter //This should throw an error, right?
print("local output value is:\(output)") // here the local output value is -1 not 2.
counter += 1
}
The first example is a case of variable shadowing. This occurs when the code has multiple scope blocks. In the first example:
let output = 0 //outer scope block
if counter < message.count {
let output = counter //inner scope block
counter += 1
} ...
In the inner scope block, a new 'output' constant has been declared with the 'let' keyword. That output constant is only valid within that 'if' block. It happens to use the same name as the one declared above the "if" statement. This is variable shadowing.
For the second example:
let output = 0
if counter < message.count {
output = counter //ERROR: Cannot assign to value: 'output' is a 'let' constant
counter += 1
} ...
The error is occurring because defining something with 'let' makes it a constant. It cannot be changed. In this case there is only one 'output' constant. It was declared above the if statement and it is a constant. So, it cannot be changed once it has been assigned a value.
So, in the first example, there are 2 "output" constants, one of which is only valid within the "if" statement. In the second example, there is only 1 "output" constant.
Curly braces constitute a scope. In an inner scope, it is always legal to overshadow a variable name from an outer scope.
let output = // ... [A]
[class, func, if, for, do, etc.] {
let output = // ... [B, overshadows A]
// *
}
You've done a self-limiting thing, in one sense, in that code at the * point may now be unable to refer to output A; it is overshadowed by output B. But it isn't illegal. On the contrary, this is an important thing to be able to do, and to disallow it would be silly.
Related
How can I define variables inside if-block ?
if ( 2 > 1 ){
val t = 6
}
print(t)
This simple code returns error : not found: value t
Your code's t variable is only defined within if block. So you can use it only inside this block. One reason for this behavior is a question: Which value has t when the condition of the if statement is false.
If you want to use the t variable outside this scope you can do the following:
Put if block in the assigning like this:
val t = if (2 > 1) 0 else 1
Use var keyword to make the t mutable and define it before if block with the default value:
var t = 1
if (2 > 1) {
t = 0
}
In any case, you need a value for else case to be able to define the t variable.
I am using TestComplete with JScript testing a webpage that has elements that I declare as a variable to make it easier to test the element later. They all have a path like:
var check1 = Window.Panel(1).Panel(2).Panel(0).Panel(0).Panel(0).Panel(0).Panel(1).Panel(0).Label(0).Checkbox(0)
The elements are dynamic, so there is no telling how many there are when the test is run. I was hoping there was some way to loop through and declare the elements, but it would involve declaring the element like this:
var check1 = Window.Panel(1).Panel(2).Panel(0).Panel(0).Panel(0).Panel(0).Panel(1).Panel(0).Label(x).Checkbox(0)
where x is the counter variable. The problem is that TestComplete sees this as a literal path and does not recognize x as a variable.
Is there any way to do this with TestComplete using JScript? Or convert a string to an object? I think I can work with that, too.
My guess is that since you store the reference in variable check1, the variable x is updated but the x in variable check1 still holds it's original value (1).
Workaround
Keep the first part of the path static in the variable, then update x and assign it to the label.
var path = Window.Panel(1).Panel(2).Panel(0).Panel(0).Panel(0).Panel(0).Panel(1).Panel(0);
// path to the Checkbox
path.Label(x).Checkbox(0);
// or if you want to loop over it
for (var x = 0, len = 8; i < len; x += 1) {
if (path.Label(x).Checkbox(0).value === 'something') {
console.log('hooray!');
}
}
I'm confused about how Swift captures mutable and immutable struct
My code downloads images asynchronously, and after the download finish, the completion closure will be called.
In the following code, It prints captured index value. The first code will print only 17. But the second code will print 0 1 2 ... 16. ( sponsorClass.count == 17 )
The firs code is
var index = 0
for sponsor in sponsorClass{
var image = AsynchronousLoadImage( sponsor.imageURL ){ loadedImage in
println("\(index)") //print 17
}
index++
}
and the second code is
var index = 0
for sponsor in sponsorClass{
let tempIndex = index
var image = AsynchronousLoadImage( sponsor.imageURL ){ loadedImage in
println("\(tempIndex)") //print 0,1,2,..,16
}
index++
}
You must think of a var or let statement as creating a variable (or constant) when the statement is executed in order to understand what's going on. The statement creates a new variable (or constant) each time it's executed.
In your first example, you create a variable named index once, before the loop starts. Each closure (you create 17 of them) “closes over” that same index variable. This means that each closure shares the index variable with the outer scope (the block that defines index) and all the other closures. There's only one “copy” of index, so when you modify it in your loop, each closure sees the modification (when the closure eventually runs).
In your second example, you create a new variable named tempIndex on each loop iteration. These 17 variables are independent of each other. Each closure closes over the variable that was created in the same loop iteration as the closure. That is, the closure created while index == 0 closes over the tempIndex that was also created while index == 0, and the closure created while index == 1 closes over the tempIndex that was created while index == 1, and so on. Thus you create 17 independent variables (each named tempIndex), and 17 closures, and each closure closes over a separate variable. Since you never modify any of the 17 tempIndex variables after you create them, each closure sees the original value assigned to its corresponding variable.
Simply put:
Let = immutable var (constant) - value CAN'T be changed.
Var = mutable ( variable) - value CAN be changed.
Why does for loop require var while for..in does not allow use of var?
for loop
for var index = 0; index < 10; i++ {
}
for..in loop
for index in "test" {
}
instead of:
for var index in "test" {
}
The Swift documentation sums it up pretty nicely:
index is a constant whose value is automatically set at the start of each iteration of the loop. As such, it does not have to be declared before it is used. It is implicitly declared simply by its inclusion in the loop declaration, without the need for a let declaration keyword.
In other words, the variable used in a for/in loop can only be a constant; thus, there's really no need to require that let be used.
The variable(s) used in a "traditional" for loop, however, can be variables or constants, so var or let is required. (Generally, they will be variables, but it is possible to use a constant in a for loop.) They can also be declared outside of the for loop (i.e., before it) and still used in the for loop. Because of this flexibility, you are required to declare it as a constant or variable.
The compiler expands for x in 0..<5 to the following:
var g = (0..<5).generate() {
while let x = g.next() {
// Use x in loop body
}
Every time around the loop, x is a freshly declared variable, the value of unwrapping the next result from calling next on the generator.
Now, that while can be rewritten in this fashion:
while var x = g.next() {
// use x here
}
I guess for this reason, for...in doesn't support the var syntax for declaring the loop counter so that it doesn't give you the wrong impression that x is mutable.
Is there any way to declare a variable at "file" scope (which will be closured by CS), without initializing it? A contrived example:
init = ->
counter = 0
inc = ->
counter += 1
This won't work, because you need to declare "counter". Adding "counter = 0" to the top would make it work, but the "= 0" is unnecessary. (A more realistic example would involve something that accesses the DOM on page load - there's no way to correctly initialize it in "file" scope.)
You'll have to define it on the outer scope, as you mentioned.
counter = null
init = ->
counter = 0
inc = ->
counter += 1
If your functions where part of an object you could use #counter, like this:
obj =
init: ->
#counter = 0
inc: ->
#counter += 1
You can say `var counter;` with backticks and that is passed literally through to the generated javascript.
When you have a problem like this, look at the generated javascript. It will be extremely clear that the variable scope is lexically limited to the function.
Looking at the generated javascript is often a good way to understand what the behavior of coffeescript constructs is.