#2049 Wrong way...

Scheren Fri 2 Nov 2012

Code:

class Foo
{
  Int x := 0
}

class Test
{
  static Void main()
  {
       f := Foo()
       echo(f.x)
       f.x := 9
  }
}

the last line, f.x := 9 is incorrect: "Expected expression statement" in other languages i used this kind of statement to modify class props. For example in C# i can simply write

f.x = 9;

what's the way in Fantom to do this simple task?

ahhatem Fri 2 Nov 2012

:= is the initialization operator, you only use it when declaring a new variable.

= is the assignment operator as usual, so you also use:

f.x = 9

This differentiation is mainly used for auto type inference without the need for a special keyword like var and avoid stupid bugs of unintended initialization of new variables.

Scheren Fri 2 Nov 2012

Very good, thank you for your explanation.

Login or Signup to reply.