Search code examples
ponylang

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


 actor Test
     fun foo(a: U32) =>
        a = a + 1

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


Solution

  • 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