Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting applications with Scala actors in practice
    text
    copied!<p>I've now written a few applications using scala actors and I'm interested in how people have approached or dealt with some of the problems I've encountered.</p> <p><strong>A plethora of Message classes or !?</strong></p> <p>I have an actor which reacts to a user operation and must cause something to happen. Let's say it <code>react</code>s to a message <code>UserRequestsX(id)</code>. A continuing problem I have is that, because I want to modularize my programs, a single actor on its own is unable to complete the action without involving other actors. For example, suppose I need to use the <code>id</code> parameter to retrieve a bunch of values and then these need to be deleted via some other actor. If I were writing a normal Java program, I might do something like:</p> <pre><code>public void reportTrades(Date date) { Set&lt;Trade&gt; trades = persistence.lookup(date); reportService.report(trades); } </code></pre> <p>Which is simple enough. However, using actors this becomes a bit of a pain because I want to avoid using <strong><code>!?</code></strong>. One actor reacts to the <code>ReportTrades(date)</code> message but it must ask a <code>PersistenceActor</code> for the trades and then a <code>ReportActor</code> to report them. The only way I've found of doing this is to do:</p> <pre><code>react { case ReportTrades(date) =&gt; persistenceActor ! GetTradesAndReport(date) } </code></pre> <p>So that in my <code>PersistenceActor</code> I have a react block:</p> <pre><code>react { case GetTradesAndReport(date) =&gt; val ts = trades.get(date) //from persietent store reportActor ! ReportTrades(ts) } </code></pre> <p>But now I have 2 problems:</p> <ol> <li>I have to create extra message classes to represent <em>the same request</em> (i.e. "report trades"). In fact I have three in this scenario but I may have many more - it becomes a problem keeping track of these</li> <li>What should I call the first and third message <code>ReportTrades</code>? It's confusing to call them both <code>ReportTrades</code> (or if I do, I must put them in separate packages). Essentially there is no such thing as <code>overloading</code> a class by <code>val</code> type.</li> </ol> <p>Is there something I'm missing? Can I avoid this? Should I just give up and use <strong><code>!?</code></strong> Do people use some organizational structure to clarify what is going on?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload