So I have this block of code on the book I'm studying. What does this independent {} actually do?
self = [super initWithImageNamed:#"character.png"];
{
self.name = playerName;
self.zPosition = 10;
}
Is this different from
self = [super initWithImageNamed:#"character.png"];
self.name = playerName;
self.zPosition = 10;
It's just scope, there is no difference in the 2 pieces of code you posted, but you could declare a short lived variable inside curly braces and scope it to just those few lines of code.
{
int x = 5;
}
NSLog("%d", x); //error
int x = 10; //legal
The first x variable goes out of scope after the curly's end, so that variable will be cleaned up. It's not a commonly used feature, but could be useful to scope certain variables. You can think of it just like an if or while block with no stipulation to enter that will run once.
Curly braces define a local scope. It can be used simply for code readability, or you can also use it to limit the scope of local variables:
For example:-
-(void)yourMethod
{
{
NSString *str=#test;
}
{
NSString *str=#testing;
}
}
So in the above you can define two same name local variable within the two scope. This is use of independent curly braces.
What Kevin said. More precisely, a group of statements surrounded by {} can be used anywhere a single statement can be. When you code, eg:
if (x == y) {
a = b;
}
you are simply applying this rule to the basic structure of:
if (<test>) <statement>
substituting { <statement_list> } for <statement>.
Likewise with for and do and so forth.
Related
In programming languages such as C you can create an anonymous code block to limit the scope of variables to inside the block can the same be done with Lua?
If so what would be the Lua equivalent of the following C code?
void function()
{
{
int i = 0;
i = i + 1;
}
{
int i = 10;
i = i + 1;
}
}
You want to use do...end. From the manual:
A block can be explicitly delimited to produce a single statement:
stat ::= do block end
Explicit blocks are useful to control the scope of variable
declarations. Explicit blocks are also sometimes used to add a return
or break statement in the middle of another block
function fn()
do
local i = 0
i = i + 1
end
do
local i = 10
i = i + 1
end
end
You can delimit a block with keyword do & end.
Reference: Programming in Lua
Running an anonymous function happens as follows:
(function(a,b) print(a+b) end)(1,4)
It outputs 5.
I've seen the answer about invoking a block that is stored in an array, but I can't get it to work with parameters.
I store the array an a part of an object, then when it's in a method, I want to invoke it, however, I need parameters.
Also, is there any limit to the parameters.
Lastly, I'd rather not use the extra storage to the variable, so invoking directly while in the array would be better.
__block int x = 123; // x lives in block storage
void (^printXAndY)(int) = ^(int y) {
x = x + y;
NSLog(#"X and Y: %d %d\n", x, y);
};
self.blocks = #[printXAndY];
printXAndY(10); // this works
void(^block)(void) = self.blocks[0];
block(); // this works
block(10); // this doesn't work
[self.blocks[0] invoke ];
The problem is this line:
void(^block)(void) = self.blocks[0];
You are declaring 'block' to take no parameters and return nothing. If you want the block to take a parameter, you need to declare it like this:
void(^block)(int) = self.blocks[0];
Note that block(); will no longer work. And when you declared the block incorrectly, that line was undefined behavior.
I have a variable in groovy like below:
project.Map
{
time.'1 1 * ?' = ['T1']
time.'2 1 * ?' = ['T2']
templates.'T1' = ['Z','X','Y']
templates.'T2' = ['Q']
}
Sorry but I am new to groovy ,when i try to access the individual
variable values in project.map how do i access them
i tried something like below
log.info(grailsApplication.config.project.Map.time[1])
log.info(grailsApplication.config.project.Map.get('time.'2 1 * ?'' ))
log.info(grailsApplication.config.project.Map.get('time[0]' ))
log.info(grailsApplication.config.project.Map.time.get('1 1 * ?'))
but they all print null value or object references.how do i access values for
time and templates both within a for loop and without it.
please see http://grails.org/doc/latest/guide/conf.html#config for the ways the config is allowed to nest. your outer syntax is especially mentioned to not be allowed:
However, you can't nest after using the dot notation. In other words, this won't work:
// Won't work!
foo.bar {
hello = "world"
good = "bye"
}
You have to write it as
project { Map { ... } }
The inner dotted parts (with the assignment) are ok (according to the doc)
I've been programming in Java for a while and I decided to try and learn Groovy. I'm going through the project euler problems and one the first problem I've already noticed something strange.
class Problem1
{
public static void main(String[] args)
{
def multiple = 1;
for(i in 1..1001)
{
//if it is divisible by three then multiply is
if(i%3 ==0)
{
multiple = multiple * i;
}
if(i%5 ==0)
{
multiple = multiple * i;
}
holder = multiple
}
println(multiple)
}
}
my value to multiple is being set incorrectly. Everything works as expected inside of the loop but when I try to print my value I get 0. It doesn't even print the 1 that I set the variable to initially. I wouldn't expect this to happen in Java. Why does it happen in Groovy? I thought that groovy was supposed to be like Java under the hood.
You're overflowing an integer (as you would in Java also)
Try using a BigInteger by changing
def multiple = 1;
To
def multiple = 1G
i have a table in lua:
enUS = {
LOCALE_STHOUSANDS = ",", --Thousands separator e.g. comma
patNumber = "%d+["..LOCALE_STHOUSANDS.."%d]*", --regex to find a number
["PreScanPatterns"] = {
["^("..patNumber..") Armor$"] = "ARMOR",
}
}
So you see there is a whole chain of self-references in this table:
LOCAL_STHOUSANDS
patNumber
["^("..patNumber..") Armor$"]
How can i perform self-referencing in an lua table?
What i don't want to do is have to hard-replace the values; there are hundreds of references:
enUS = {
LOCALE_STHOUSANDS = ",", --Thousands separator e.g. comma
patNumber = "%d+[,%d]*", --regex to find a number
["PreScanPatterns"] = {
["^(%d+[,%d]*) Armor$"] = "ARMOR",
}
}
How can i perform self-referencing in an lua table?
You don't.
Lua is not C. Until the table is constructed, none of the table entries exist. Because the table itself doesn't exist yet. Therefore, you can't have one entry in a table constructor reference another entry in a table that doesn't exist.
If you want to cut down on repeated typing, then you should use local variables and do/end blocks:
do
local temp_thousands_separator = ","
local temp_number_pattern = "%d+["..LOCALE_STHOUSANDS.."%d]*"
enUS = {
LOCALE_STHOUSANDS = temp_thousands_separator, --Thousands separator e.g. comma
patNumber = "%d+["..temp_thousands_separator.."%d]*", --regex to find a number
["PreScanPatterns"] = {
["^("..temp_number_pattern..") Armor$"] = "ARMOR",
}
}
end
The do/end block is there so that the temporary variables don't exist outside of the table creation code.
Alternatively, you can do the construction in stages:
enUS = {}
enUS.LOCALE_STHOUSANDS = ",", --Thousands separator e.g. comma
enUS.patNumber = "%d+["..enUS.LOCALE_STHOUSANDS.."%d]*", --regex to find a number
enUS["PreScanPatterns"] = {
["^("..enUS.patNumber..") Armor$"] = "ARMOR",
}
There's no way of doing this inside the constructor itself, but you can do it after creating the table like so:
enUS = {
LOCALE_STHOUSANDS = ","
}
enUS.patNumber = "%d+["..enUS.LOCALE_STHOUSANDS.."%d]*"
enUS.PreScanPatterns = {
["^("..enUS.patNumber..") Armor$"] = "ARMOR",
}
If you specifically need to refer to the current table, Lua provides a "self" parameter, but it's only accessible in functions.
local t = {
x = 1,
y = function(self) return self.x end
}
-- this is functionally identical to t.y
function t:z() return self.x end
-- these are identical and interchangeable
print(t:y(), t.z(t))
-- 1, 1