Repository navigation
Conversation
| tag: Tag[Poll[V]], | ||
| frame: Frame | ||
| ): (Maybe[V] => A < (Poll[V] & S)) < S = | ||
| ): Either[A, Maybe[V] => A < (Poll[V] & S)] < S = |
There was a problem hiding this comment.
The API was hiding the information if the poll had finished or not
| val defaultCapacity = | ||
| import AllowUnsafe.embrace.danger | ||
| given Frame = Frame.internal | ||
| IO.Unsafe.evalOrThrow(System.property[Int]("kyo.actor.capacity.default", 100)) |
There was a problem hiding this comment.
How about a power of 2?
| // Create an unbounded channel to serve as the actor's mailbox | ||
| // Unbounded capacity in this initial prototype |
There was a problem hiding this comment.
no, I've removed it
| * @tparam B | ||
| * The type of result this actor produces upon completion | ||
| */ | ||
| abstract class Actor[+E, A, B]: |
| * @return | ||
| * The actor's final result of type B | ||
| */ | ||
| def result(using Frame): B < (Async & Abort[Closed | E]) = fiber.get |
There was a problem hiding this comment.
Maybe this should be await?
…uce selfWith + rename resent to reenqueue
…when parameters + introduce SubjectTest
| */ | ||
| def init[E, A](promise: Promise[E, A])(using frame: Frame): Subject[A] = | ||
| def tryComplete(r: A) = | ||
| Abort.whenNot(promise.complete(Result.succeed(r)))(Closed("Subject", frame)) |
There was a problem hiding this comment.
I've updated Abort.when to allow effects in any of its parameters. Not allowing effects in the condition makes the API not very useful. I've also added Abort.whenNot.
| * @return | ||
| * A unit computation that may fail if the condition is false | ||
| */ | ||
| inline def whenNot[E, S](b: Boolean < S)(inline value: => E < S)(using inline frame: Frame): Unit < (Abort[E] & S) = |
There was a problem hiding this comment.
How about calling this unless?
| for | ||
| mailbox <- | ||
| // Create a bounded channel to serve as the actor's mailbox | ||
| Channel.init[A](Int.MaxValue, Access.MultiProducerSingleConsumer) |
There was a problem hiding this comment.
| Channel.init[A](Int.MaxValue, Access.MultiProducerSingleConsumer) | |
| Channel.init[A](capacity, Access.MultiProducerSingleConsumer) |
| * @return | ||
| * A Subject[A] that can be used to send messages to this actor | ||
| */ | ||
| val subject: Subject[A] |
There was a problem hiding this comment.
Any reason these are val in the class definition?
There was a problem hiding this comment.
It was a lazy way to allow export subject.* so the methods are available in the actor instance directly. I've changed it to use constructor params + avoid val.
|
@hearnadam can we merge this? |
Problem
Dealing with effects that can't be directly forked like a local
Varisn't trivial and, in some cases,Isolates can't even be provided due to the specific semantics of effects. For example, I'm working on aBrowsereffect that uses playwright to interact with a browser. Given that playwright isn't thread-safe, providing anIsolatewould require a more advanced mechanism like forking multiple playwright engines, which doesn't seem desirable.Solution
Actors provide a straightforward way to isolate a computation with arbitrary effects and state. Inputs can be serially consumed via
Pollwhile keeping the complete state of the computation, including its effect handlers.Streamprovides a similar solution viafoldbut the execution model isn't very flexible. Unlike streaming,Actorreferences can be freely passed around and receive messages in multiple code paths. The implementation also provides supervision hierarchy, which is a more convenient way to handle resources.Notes
This is based on the collaboration with @DamianReeves for Functional Scala last year. Thank you!