Returning a function

Good morning,

I am trying to convert the following Erlang code to Scala,

PID = spawn(fun() →
receive
mon_start → apply(M, F, A),
?INFO(“System to be monitored started.”)
end

Does anybody have any suggestions please ?

Thanks a lot and good day :slight_smile:

It would be easier if you explain what that code does.

This code, spawns a process whose main responsabilities are to :

  1. receive a message of the form mon_start, and if the receive criteria is met, then an apply method is invoked with the parameters M,F,A.

  2. A information log statement is outputted on screen.

  3. Ends the process work

Thanks a lot and good day :slight_smile:

Well the problem is that here you do not want a code translation but an equivalent tool or a design question, since the logic is as simple as:

val msg = receiveMessage()
if (satisfyCriteria(msg)) {
  callOtherMethod(foo, bar, baz)
}
println("Log")

Now the problem is not the logic, but that you do not want it to be synchronous.

If you want something really close to erlang actors then you may look into Akka and if you have problems with that ask in an Akka focused group; like this Gitter channel.

Or if you are not really fond of actors and just want to make the process asynchronous you may take a look at Futures.

Other alternatives may include (reactive) streams and / or IO monads and their concurrent capabilities.

Overall, your question is too abstract to really provide a better answer.

1 Like

Thanks a lot for your help.

I will look into your proposed solutions and tackle it using one of the provided solutions

Thanks a lot and good day :slight_smile: