Is it possible to modify a function argument? (like & in C++)

117 Views Asked by At
 actor Test
     fun foo(a: U32) =>
        a = a + 1

I want test.foo(a) to modify a. Is this possible? Thanks

1

There are 1 best solutions below

2
On BEST ANSWER

You can only modify vars at a class level. This is intentional because actors don't like in-place updates -- it really doesn't jive well with lock-free concurrency.

Functions, by default, have the box capability, which means that data manipulated by this function are read-only. To ensure that the function can mutate data, the method will need to be declared fun ref.

actor Main
  var i: U32 = 0

  fun ref foo() =>
    i = i + 1

  new create(env: Env) =>
    env.out.print(i.string())
    foo()
    env.out.print(i.string())

Playground