F # Mailbox vs MailboxProcessor

I noticed that the mailbox type is encapsulated and can only be used with the mail server.

This means that for an agent to whom I can send messages, I have to have one mailbox of the same type (or use the existing MailboxProcessor in an exotic way).

Should I understand that having multiple mailboxes for the same workflow will inherently lead to poor design? Ccr clearly gives you this level of freedom.

Edit: As Daniel noted, if someone wants to send several types of messages, DU elegantly solves the problem - and it is not as if I did not do this in the past.

But the question is, does that smell code? Would you add more types of messages sent to the agent over time so that you have too many responsibilities? Sometimes I think it would be important to always encapsulate the types of messages that the agent consumes behind the interface, so this information is never displayed.

0
source share
4 answers

I think I found what I was looking for. I have listened to Rich Hickey’s conversation (we are still there) at least 5 times, and I believe that his approach solves many of the design problems that I had. Obviously, this can be implemented using F # Mailboxes or CAS links.

I really recommend it and will be happy to hear some feedback.

0
source

, F #, MailboxProcessor CCR, , , , , , F #, . CCR, , , COmega ( MSR).

, COmega F #:

public class OnePlaceBuffer {
  private async empty();
  private async contains(string s);

  public OnePlaceBuffer() { empty(); }
  public void Put(string s) & empty() {
    contains(s);
  }
  public string Get() & contains(string s) {
    empty();
    return s;
  }
}

async ( : empty, contains, Put Get), , , (.. ). F # MailboxProcessor :

type Message<'T> =
  | Put of 'T * AsyncReplyChannel<unit>
  | Get of AsyncReplyChannel<'T>

MailboxProcessor.Start(fun agent ->
  let rec empty = agent.Scan(function
    | Put(v, repl) -> repl.Reply(); Some(full(v))
    | _ -> None)
  and full v = agent.Scan(function
    | Get repl -> repl.Reply(v); Some(empty)
    | _ -> None)
  empty )

, . F #, empty full - , , , , ( ). COmega .

, , , F # MailboxProcessor, ...

, , MailboxProcessor F #, , , , - . , , MailboxProcessor (, , , ). . .

+4

, . ?

+1

, - , , - ISubject Reactive Extensions. , . , , :

  • - , , , , , , ..
  • - (Put/Get) - .

You are right in thinking that you most likely want to restrict data messages to a specific type, but technically, DU is one type with many alternatives. If you would take the same approach as Luca with its original dynamic approach in L'Agent, I think that we will agree that too many types in one mailbox is a challenge.

+1
source

All Articles