There are two solutions, both of them use explicit recursive loop definition, main concept of Akka F# actors.
First you may define variables, which should be visible only inside actor's scope, before loop definition (in example below I've changed i
definition to reference cell, because mutable variables cannot be captured by closures):
let actorRef =
spawn system "my-actor" <| fun mailbox ->
let i = ref 1
let rec loop () =
actor {
let! msg = mailbox.Receive()
match msg with
| Some x -> i := !i + x
| None -> ()
return! loop()
}
loop()
However, more advised solution is to keep your state immutable during message handling, and change it only when passing in next loop calls, just like this:
let actorRef =
spawn system "my-actor" <| fun mailbox ->
let rec loop i =
actor {
let! msg = mailbox.Receive()
match msg with
| Some x -> return! loop (i + x)
| None -> return! loop i
}
loop 1 // invoke first call with initial state
ReceiveActor
? Something that'd enable you to use a recursive poll loop? – Disposable