Coder Social home page Coder Social logo

Comments (1)

louthy avatar louthy commented on August 22, 2024

Unfortunately it's only a Union type in name. I find it easiest to do this:

Declare your messages like so:

        public class Msg { }
        public class Add : Msg { }
        public class Remove : Msg { }
        public class Show : Msg { }

Then create a static Inbox method to take a Msg, which uses the ternary operators and pattern matching on the types to make something that looks like a match statement in functional programming:

        public static class MyActor
        {
            public static Unit Inbox(Msg msg) =>
                msg is Add a    ? Add(a)
              : msg is Remove r ? Remove(r)
              : msg is Show s   ? Show(s)
              : unit;

            static Unit Add(Add msg)
            {
                // Use Add message
                return unit;
            }

            static Unit Remove(Remove msg)
            {
                // Use Remove message
                return unit;
            }

            static Unit Show(Show msg)
            {
                // Use Show message
                return unit;
            }
        }

Because the Inbox returns Unit and not void, you will need to wrap it with act when spawning:

        spawn("my-actor", act<Msg, Unit>(MyActor.Inbox));

Or make the Inbox less attractive and return void:

            public static void Inbox(Msg msg)
            {
                var _ = msg is Add a    ? Add(a)
                      : msg is Remove r ? Remove(r)
                      : msg is Show s   ? Show(s)
                      : unit;
            }

I need to update those samples, as this approach is much easier these days (with C#7).

from echo-process.

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.