diff --git a/documentation/wiki/FSharp API.md b/documentation/wiki/FSharp API.md index 4f4918b399f..63757786992 100644 --- a/documentation/wiki/FSharp API.md +++ b/documentation/wiki/FSharp API.md @@ -65,6 +65,8 @@ Paragraph above already has shown, how actors may be created with help of the sp - `spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef` - spawns an actor using specified actor computation expression. The actor can only be used locally. - `spawnOpt (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, with custom spawn option settings. The actor can only be used locally. - `spawne (actorFactory : ActorRefFactory) (name : string) (expr : Expr -> Cont<'Message, 'Returned>>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, using an Expression AST. The actor code can be deployed remotely. +- `spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef` - spawns an actor using specified actor quotation. The actor can only be used locally. +- `spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor quotation, with custom spawn option settings. The actor can only be used locally. All of these functions may be used with either actor system or actor itself. In the first case spawned actor will be placed under */user* root guardian of the current actor system hierarchy. In second option spawned actor will become child of the actor used as [actorFactory] parameter of the spawning function. diff --git a/src/core/Akka.FSharp/FsApi.fs b/src/core/Akka.FSharp/FsApi.fs index 41d4b22e379..d319e2d83c4 100644 --- a/src/core/Akka.FSharp/FsApi.fs +++ b/src/core/Akka.FSharp/FsApi.fs @@ -266,6 +266,7 @@ module Logging = module Linq = open System.Linq.Expressions + open Microsoft.FSharp.Linq let (|Lambda|_|) (e : Expression) = match e with @@ -285,13 +286,17 @@ module Linq = | _ -> None let (|Ar|) (p : System.Collections.ObjectModel.ReadOnlyCollection) = Array.ofSeq p - - type Expression = - static member ToExpression(f : System.Linq.Expressions.Expression>>) = + + let toExpression<'Actor>(f : System.Linq.Expressions.Expression) = match f with - | Lambda(_, Invoke(Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |]))) -> - Expression.Lambda(p, [||]) :?> System.Linq.Expressions.Expression>> + | Lambda(_, Invoke(Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |]))) + | Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |]) -> + Expression.Lambda(p, [||]) :?> System.Linq.Expressions.Expression> | _ -> failwith "Doesn't match" + + type Expression = + static member ToExpression(f : System.Linq.Expressions.Expression>>) = toExpression> f + static member ToExpression<'Actor>(f : Quotations.Expr<(unit -> 'Actor)>) = toExpression<'Actor> (QuotationEvaluator.ToLinqExpression f) module Serialization = open Nessos.FsPickler @@ -411,7 +416,7 @@ module Spawn = actorFactory.ActorOf(props, name) /// - /// Spawns an actor using specified actor computation expression, with custom actor Props settings. + /// Spawns an actor using specified actor computation expression, with custom spawn option settings. /// The actor can only be used locally. /// /// Either actor system or parent actor @@ -434,6 +439,30 @@ module Spawn = let spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef = spawnOpt actorFactory name f [] + /// + /// Spawns an actor using specified actor quotation, with custom spawn option settings. + /// The actor can only be used locally. + /// + /// Either actor system or parent actor + /// Name of spawned child actor + /// Used to create a new instance of the actor + /// List of options used to configure actor creation + let spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) + (options : SpawnOption list) : ActorRef = + let e = Linq.Expression.ToExpression<'Actor> f + let props = applySpawnOptions (Props.Create e) options + actorFactory.ActorOf(props, name) + + /// + /// Spawns an actor using specified actor quotation. + /// The actor can only be used locally. + /// + /// Either actor system or parent actor + /// Name of spawned child actor + /// Used to create a new instance of the actor + let spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef = + spawnObjOpt actorFactory name f [] + /// /// Wraps provided function with actor behavior. /// It will be invoked each time, an actor will receive a message. diff --git a/src/core/Akka.FSharp/README.md b/src/core/Akka.FSharp/README.md index 74bb851ece7..8b902790b27 100644 --- a/src/core/Akka.FSharp/README.md +++ b/src/core/Akka.FSharp/README.md @@ -55,6 +55,8 @@ Paragraph above already has shown, how actors may be created with help of the sp - `spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef` - spawns an actor using specified actor computation expression. The actor can only be used locally. - `spawnOpt (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, with custom spawn option settings. The actor can only be used locally. - `spawne (actorFactory : ActorRefFactory) (name : string) (expr : Expr -> Cont<'Message, 'Returned>>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, using an Expression AST. The actor code can be deployed remotely. +- `spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef` - spawns an actor using specified actor quotation. The actor can only be used locally. +- `spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor quotation, with custom spawn option settings. The actor can only be used locally. All of these functions may be used with either actor system or actor itself. In the first case spawned actor will be placed under */user* root guardian of the current actor system hierarchy. In second option spawned actor will become child of the actor used as [actorFactory] parameter of the spawning function.