Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatch functions

What exactly are dispatch functions? I've googled them and all is vague. They seem to just be nested blocks/closures inside of other functions? Speaking from a scala/lift point..but i assume it's universal, i've seen them mentioned in ruby as well.

like image 504
iAmDecim Avatar asked May 18 '10 06:05

iAmDecim


People also ask

What is the purpose of dispatching a function?

The goal of dispatching is to dynamically decide what to do in your function. When you have a (dynamic) dispatch function it's main (or only, if you don't need casting or other conversions) responsibility is to decide which other function to call.

What is the difference between dispatch and reducer?

In that same scope is the reducer function that we have written and passed into createStore or useReducer. So the dispatch function also has access to reducer - our reducer function (no matter what we called it) that we passed in. That means that the dispatch function can invoke the reducer function.

Do we need to dispatch our actions in react?

An object with the same function names, but with every action creator wrapped into a dispatch call so they may be invoked directly, will be merged into the component’s props. The above implies that we do not need to dispatch our actions.

What is dispatching in manufacturing?

Dispatching is the physical handing over of a manufacturing order to the operating facility (a worker) through the release of orders and instructions in accordance with a previously developed plan of activity (time and sequence) established by the scheduling section of the production planning and control department.


2 Answers

The goal of dispatching is to dynamically decide what to do in your function.

When you have a (dynamic) dispatch function it's main (or only, if you don't need casting or other conversions) responsibility is to decide which other function to call. The decision is often based on the type of the instance the method is called on, or the type of some of the parameters, but it can also depend e.g. on the value of the parameter(s), or some config values.

The dispatching rule can be hardcoded (using e.g. pattern matching in scala), or may come from a dispatch table.

As you mentioned there are several variations, like single dispatch (concrete method depends on the instance the original method is called on, which is a basic OO mechanism), double dispatch (dispatches a function call to different concrete functions depending on the runtime types of multiple objects involved in the call).

A related design pattern is the Visitor, which allows you to add a set of functions dynamically to existing classes and which also has dynamic dispatch at its core.

The nested blocks/closures appear when you define the concrete method inside of the dispatch method, or in some initialization code (e.f. for the dispatch table).

A simple example for the case when dispatching is based on the value of the parameter, with hardcoded decision and with dispatch table:

   class Dispatch {

     def helloJohn(): String = "Hello John"

     def helloJoe(): String = "Hello Joe"

     def helloOthers(): String = "Hello"

     def sayHello(msg: String): String = msg match {
       case "John" => helloJohn()
       case "Joe" => helloJoe()
       case _ => helloOthers()
     }


     val fs = Map("John" -> helloJohn _, "Joe" -> helloJoe _)

     def sayHelloDispatchTable(msg: String): String = fs.get(msg) match {
         case Some(f) => f()
         case _ => helloOthers()
     }
   }
like image 86
Sandor Murakozi Avatar answered Sep 20 '22 15:09

Sandor Murakozi


Dispatch is the term Lift uses for dispatching web services requests.

The easiest way to define the dispatch function using RestHelper (see http://www.assembla.com/wiki/show/liftweb/REST_Web_Services )

For example:

object MyRestService extends RestHelper {
  serve {
    case "api" :: "user" :: AsLong(id) :: _ XmlGet _ => <b>ID: {id}</b>
    case "api" :: "user" :: AsLong(id) :: _ JsonGet _ => JInt(id)
  }
}

Then in Boot.scala:

LiftRules.dispatch.append(MyRestService)

Hope this helps.

like image 45
David Pollak Avatar answered Sep 18 '22 15:09

David Pollak