Search code examples
hpcchapel

Callback functions in Chapel


I have the following Chapel code.

proc update(x: int(32)) {
  return 2*x;
}

proc dynamics(x: int(32)) {
  return update(x);
}

writeln(dynamics(7));

I would like to send some kind of callback to dynamics, like

proc update(x: int(32)) {
  return 2*x;
}

proc dynamics(x: int(32), f: ?) {
  return f(x);
}

writeln(dynamics(7, update));

Is this possible? Are there examples I could browse?


Solution

  • Chapel has first-class functions . They are work in progress, at the same time have been used successfully (details are escaping me).

    Your example works if you either remove the :? or specify the function's type as func(int(32), int(32)):

    proc dynamics(x: int(32), f) // or proc dynamics(x: int(32), f: func(int(32), int(32)))