You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "eloots (via GitHub)" <gi...@apache.org> on 2023/10/16 20:21:59 UTC

[PR] Add section on using Scala 3 Union types to eliminate msg adapters [incubator-pekko]

eloots opened a new pull request, #741:
URL: https://github.com/apache/incubator-pekko/pull/741

   - Use InteractionPatternsSpec as an example for applying Scala 3's Union types to symplify actor code
     - Remove response message wrapper & adapter
     - Use the union of the actor's public protocol (the `Translate` message (the only member of the `Command` ADT) and the possible responses from the backend (messages `JobStarted`, `JobProgress`, and `JobCompleted`): `private type CommandAndResponse = Command | Backend.Response`
     - Instead of utilising the message adaptor `ActorRef` in the `replyTo` field of the message sent to the backend, `context.self` is used instead
     - The internal (extended) `Behavior[CommandAndResponse]` is narrowed to `Behavior[Command]` at creation time
   
   The diffs between the original and the new version:
   
   ```bash
   136d139
   <         private final case class WrappedBackendResponse(response: Backend.Response) extends Command
   137a141,142
   >         private type CommandAndResponse = Command | Backend.Response
   >
   139,141c144
   <           Behaviors.setup[Command] { context =>
   <             val backendResponseMapper: ActorRef[Backend.Response] =
   <               context.messageAdapter(rsp => WrappedBackendResponse(rsp))
   ---
   >           Behaviors.setup[CommandAndResponse] { context =>
   143,144c146,147
   <             def active(inProgress: Map[Int, ActorRef[URI]], count: Int): Behavior[Command] = {
   <               Behaviors.receiveMessage[Command] {
   ---
   >             def active(inProgress: Map[Int, ActorRef[URI]], count: Int): Behavior[CommandAndResponse] = {
   >               Behaviors.receiveMessage[CommandAndResponse] {
   147c150
   <                   backend ! Backend.StartTranslationJob(taskId, site, backendResponseMapper)
   ---
   >                   backend ! Backend.StartTranslationJob(taskId, site, context.self)
   150,162c153,162
   <                 case wrapped: WrappedBackendResponse =>
   <                   wrapped.response match {
   <                     case Backend.JobStarted(taskId) =>
   <                       context.log.info("Started {}", taskId)
   <                       Behaviors.same
   <                     case Backend.JobProgress(taskId, progress) =>
   <                       context.log.info2("Progress {}: {}", taskId, progress)
   <                       Behaviors.same
   <                     case Backend.JobCompleted(taskId, result) =>
   <                       context.log.info2("Completed {}: {}", taskId, result)
   <                       inProgress(taskId) ! result
   <                       active(inProgress - taskId, count)
   <                   }
   ---
   >                 case Backend.JobStarted(taskId) =>
   >                   context.log.info("Started {}", taskId)
   >                   Behaviors.same
   >                 case Backend.JobProgress(taskId, progress) =>
   >                   context.log.info2("Progress {}: {}", taskId, progress)
   >                   Behaviors.same
   >                 case Backend.JobCompleted(taskId, result) =>
   >                   context.log.info2("Completed {}: {}", taskId, result)
   >                   inProgress(taskId) ! result
   >                   active(inProgress - taskId, count)
   167c167
   <           }
   ---
   >           }.narrow
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Add section on using Scala 3 Union types to eliminate msg adapters [incubator-pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #741:
URL: https://github.com/apache/incubator-pekko/pull/741#issuecomment-1765217192

   backport of 0a09ccc71e28efac5250d448d2e65698b83f508c


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Add section on using Scala 3 Union types to eliminate msg adapters [incubator-pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning merged PR #741:
URL: https://github.com/apache/incubator-pekko/pull/741


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org