4 people like it.

Generic agent post operator

Generic operator that introduces a convenient syntax for "posting" messages into any agent that has Post member. Specifically, it's useful for the standard MailboxProcessor and the AutoCancelAgent (from FSharpx library).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
open FSharp.Control

let inline (<--) (agent: ^a) (msg: 'b) =
    (^a: (member Post: 'b -> unit) (agent, msg))

let test =
    let mailboxProcessor = MailboxProcessor.Start(fun inbox -> async { return () }) 
    let autoCancelAgent = AutoCancelAgent.Start(fun inbox -> async { return () })

    mailboxProcessor <-- ()
    autoCancelAgent <-- ()
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Control

--------------------
namespace Microsoft.FSharp.Control
val agent : 'a (requires member Post)
val msg : 'b
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val test : unit

Full name: Script.test
val mailboxProcessor : MailboxProcessor<unit>
Multiple items
type MailboxProcessor<'Msg> =
  interface IDisposable
  new : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:CancellationToken -> MailboxProcessor<'Msg>
  member Post : message:'Msg -> unit
  member PostAndAsyncReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> Async<'Reply>
  member PostAndReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> 'Reply
  member PostAndTryAsyncReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> Async<'Reply option>
  member Receive : ?timeout:int -> Async<'Msg>
  member Scan : scanner:('Msg -> Async<'T> option) * ?timeout:int -> Async<'T>
  member Start : unit -> unit
  member TryPostAndReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> 'Reply option
  ...

Full name: Microsoft.FSharp.Control.MailboxProcessor<_>

--------------------
new : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:System.Threading.CancellationToken -> MailboxProcessor<'Msg>
static member MailboxProcessor.Start : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:System.Threading.CancellationToken -> MailboxProcessor<'Msg>
val inbox : MailboxProcessor<unit>
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val autoCancelAgent : AutoCancelAgent<unit>
type AutoCancelAgent<'T> =
  interface IDisposable
  private new : mbox:Agent<'T> * cts:CancellationTokenSource -> AutoCancelAgent<'T>
  member Post : m:'T -> unit
  member PostAndAsyncReply : buildMessage:(AsyncReplyChannel<'a1> -> 'T) * ?timeout:int -> Async<'a1>
  member PostAndReply : buildMessage:(AsyncReplyChannel<'a1> -> 'T) * ?timeout:int -> 'a1
  member PostAndTryAsyncReply : buildMessage:(AsyncReplyChannel<'a1> -> 'T) * ?timeout:int -> Async<'a1 option>
  member Receive : ?timeout:int -> Async<'T>
  member Scan : scanner:('T -> Async<'a1> option) * ?timeout:int -> Async<'a1>
  member TryPostAndReply : buildMessage:(AsyncReplyChannel<'a1> -> 'T) * ?timeout:int -> 'a1 option
  member TryReceive : ?timeout:int -> Async<'T option>
  ...

Full name: FSharp.Control.AutoCancelAgent<_>
static member AutoCancelAgent.Start : f:(MailboxProcessor<'T> -> Async<unit>) -> AutoCancelAgent<'T>
Raw view Test code New version

More information

Link:http://fssnip.net/hI
Posted:11 years ago
Author:Vasily Kirichenko
Tags: agents