Git Product home page Git Product logo

Comments (3)

yevhen avatar yevhen commented on September 23, 2024

Just use nested match like here

P.S. Generic actor grains were deprecated since 2.x exactly due to this reason (we can’t have DU with system messages). DU are only for public interface, ie caller side type safety.

from orleankka.

Neftedollar avatar Neftedollar commented on September 23, 2024

Ok, maybe I should write something like

type GrainMessage<'t> = | Msg of 't | Timer of Timer  | ..
type TypedGrain<'t> () =
 inherit  ActorGrain() 
 abstract member this.TypedReceive ( msg: GrainMessage<'t>)
 override this.Receive (message) =
  match message with 
  | :? 't as msg -> this.TypedReceive(GrainMessage(msg))
  | :? Timer as msg -> this.TypedReceive(GrainMessage ( msg))
 .. etc

and then use it like ActorGrain but with TypedReceive method instead of Receive.
πŸ€”

from orleankka.

Neftedollar avatar Neftedollar commented on September 23, 2024

I've created this

type ActorMsg<'t> =
    | Msg of 't
    | Timer of Orleankka.Timer
    | Reminder of Orleankka.Reminder
    | Activate of Orleankka.Activate
    | Deactivate of Orleankka.Deactivate

[<AbstractClass>]
type TypedActor<'a>() =
    inherit ActorGrain()
    abstract member ReceiveT: ActorMsg<'a> -> System.Threading.Tasks.Task<obj> 
    override this.Receive(msg:obj) = task {
      match msg with
      | :? 'a as m -> return! this.ReceiveT (Msg m)
      | :? Orleankka.Timer as t -> return! this.ReceiveT( Timer t)
      | :? Orleankka.Activate as a -> return! this.ReceiveT ( Activate a)
      | :? Orleankka.Deactivate as d -> return! this.ReceiveT( Deactivate d)
      | _ -> return unhandled() }

and use it like

type HelloGrain (loggerFactory:ILoggerFactory) =
  inherit TypedActor<HelloMessages>()
  let log = loggerFactory.CreateLogger(typeof<HelloGrain>)
  interface IHello
  override this.ReceiveT(msg) = task {
    match msg with
    | Msg(Hi)-> 
        log.LogInformation("Client asked to say Hi!")
        return some "Oh, hi!"
    | Msg(Hello s) ->
        log.LogInformation (sprintf "Client asked to say Hello to %s" s)
        return sprintf "Hello, %s" s |> some
    | Msg(Bue) -> 
        log.LogInformation ("Client wants to go")
        return none()
    | _ ->  return unhandled()
  }

and it's awesome!

from orleankka.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.