scala - Actor based classes with or without interfaces -


i playing around scala right , tried figure out best practices how design classes. (trying scala since week or so.)

since erlang time huge fan of message passing , actor based software. in scala examples actor classes implemented this:

object foo object bar class myactor extends actor {   def receive = {     case foo => ...     case bar => ...     case _ => ...   } } 

but learned object oriented (interfaces , polymorphism) carrier tells me concept not flexible.

myactor replaced myadvancedactor there no contract defines messages myactor implementation needs implement.

when think writing actors in scala tend write trait specifies methods. myactor implementation needs implement methods in can send own private messages itself. approach have specified interface , can replace myactor implementation in type-safe manner.

in time of reading scala tutorials , examples did not come across such class design. not common sense or there better ways in doing in scala? or these tutorials small cover such topic?

common practice use algebraic data type in such cases: create sealed base type messages this:

sealed trait myactormessages object foo extends myactormessages object bar extends myactormessages 

but kind of contract not enforced compiler. use use typed channels enforce contract:

class myactor extends actor channels[tnil, (myactormessages, myactorreply) :+: tnil] {   channel[myactormessages] { (req, snd) ⇒     req match {       case foo  ⇒ ...       case bar ⇒ ... // you'll warning if forget `bar`     }   } } 

compiler force (with warning) process possible message types (in case subtypes of myactormessages), , senders forced send valid messages using <-!- method (with compilation error).

note senders can use unsafe method ! send invalid messages.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -