#1965 the ':=' operator

shosh Thu 19 Jul 2012

why does Fantom use := to initialize a variable? the compiler knows where its first instant is and can infer its type with only using the = operator.. any insights?

brian Thu 19 Jul 2012

Because in languages like Python or Ruby is you mistype a variable later you create a new one and it can cause a lot of confusion. Declaring a new variable versus assigning to an existing one is two different operations and the code should capture that intent clearly

shosh Thu 19 Jul 2012

i didn't quite understand when you said:

"if you mistype a variable later you create a new one and it can cause a lot of confusion" can you explain?

helium Thu 19 Jul 2012

Without :=

Void foo()
{
   bla = 42
   ...
   while (true)
   {
      blo = 7  // oops, typo, should be bla = 7, but instead I created a new variable
      bar(bla) // called with wrong value
   }
   ...
}

with :=

Void foo()
{
   bla := 42
   ...
   while (true)
   {
      blo = 7  // compiler error, blo not in scope
      bar(bla)
   }
   ...
}

shosh Thu 19 Jul 2012

that clears it up.. thank you!

Login or Signup to reply.