#1957 variables initialization

fantlan Sun 15 Jul 2012

i found something interesting:

see the code:
class mats
{
static Void foo(Int x, Int z)
{
z1:=z
x1:=x
t1:=4
{
y:=3
// x1:=1 if i add this line there is an error that it was already defined
t1:=3
echo("this is the value in x1"+x1+"this is the value in z1" +z1+ "this is the value       in t1"+t1)
}
}
static Void main(Str[] args)
{
foo(4,5)
}}

how come in t1 it doesn't give the same error?? i saw that for the last variable initialized in the first block we can define it again in a sub-block but all other variables no...

anyone can explain why?

andy Sun 15 Jul 2012

@fantlan - can you markup that code properly using Fandoc - two spaces before each line will do the trick - a bit hard to read ;)

fantlan Sun 15 Jul 2012

ok thanks changed!

KevinKelley Sun 15 Jul 2012

That's getting treated as an it-block applied to the t1:=4 line, causing really strange errors -- I get a verifyError from the JVM.

If you put in explicit semicolons, it's more clear where the error is.

class mats
{
  static Void main(Str[] args)
  {
    x1 := 1;
    {
      x1 := 2 // error, already defined
    }
  }
}

Basically braces are not a standalone operator that creates a scope, but should only appear as part of some other syntactic element.

fantlan Mon 16 Jul 2012

i understand the error.. and i actually wrote the code to check if braces where a standalone operator that creates a scope... i understood that its not but what is interesting is that it doesnt give the same error with t1. why for t1 it doesnt say "already defined".. or for anyone there is error or for all no?

Login or Signup to reply.