Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

passing/converting Elmish `dispatch` messages from a parent component to a child component

In the wonderful FBlazorShop repo, Onur Gumus is riffing off of Steve Sanderson’s Pizza Workshop with F# flavor. On line 128 of blob/master/FBlazorShop.Web.BlazorClient/Home/Home.fs [GitHub], Onur is passing an Elmish Message for the parent, HomeView, inheriting ElmishComponent<Model, Message> , to a child, PizzaConfigView, inheriting ElmishComponent<Model, PizzaConfigMsg>. By convention, we can see Message being converted (?) to PizzaConfigMsg with this:

(PizzaConfigMsg >> dispatch)

where dispatch is of type Message -> unit. At the time of this writing, I have no idea how this ‘conversion’ is happening (in part because I refuse to compile this repo by going back to .NET core 3.x). I am not familiar with this usage of the >> operator. Is this operation actually a conversion or is something else going on?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you find >> confusing, but are comfortable with |>, then you can easily rewrite that line like this:

fun pizzaMsg -> pizzaMsg |> PizzaConfigMsg |> dispatch

Since the top-level message type is:

type Message =
    | SpecialsReceived of PizzaSpecial list
    | PizzaConfigMsg of PizzaConfigMsg
    | OrderMsg of OrderMsg
    | CheckoutRequested of Order

This tells us that the code is converting a value of type PizzaConfigMsg (which I’ve called pizzaMsg above) to a top-level Message via the Message.PizzaConfigMsg union case, then dispatching the result.

This style of coding (where function arguments become implicit, rather than explicit) is called "point-free programming". You can find more information about it here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading