Submission Functions

What are sending functions? I looked for them, and everything is vague. It seems like they are just nested blocks / closures inside other functions? Speaking from the point of scala / lift ... but I assume it is universal, I saw how they were mentioned in ruby.

+6
function scala lift
source share
2 answers

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

If you have a (dynamic) dispatch function main (or only if you do not need casting or other transformations), then the responsibility is to decide which other function to call. This decision is often based on the type of instance on which the method is called, or on the type of some parameters, but may also depend, for example, on the value of the parameter (s) or some configuration values.

A scheduling rule can be hard-coded (for example, using pattern matching in scala) or can be obtained from a scheduling table.

As you mentioned, there are several options, such as a separate dispatch (the specific method depends on the instance on which the original method is called, which is the main OO mechanism), double dispatch (sends a function call to various specific functions depending on the execution time of several objects participating in the call).

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

Nested blocks / closures appear when you define a specific method inside the send method or in some initialization code (for example, for a distribution table).

A simple example for the case when scheduling is based on a parameter value, with a hard-coded solution and with a scheduling 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() } } 
+7
source share

Submission is a term that is used to send requests to web services.

The easiest way to define a send 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.

+3
source share

All Articles