1

I have been unable to determine what the below code represents, as these syntax are not entirely listed in Scala documentation. Can someone shed some light on each of the below line? If the above title needs to be changed to something that can benefit others, please let me know.

val route = path("hello") { get { complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>")) } } 

Ref: http://doc.akka.io/docs/akka-http/current/scala/http/introduction.html#http-client-api

2
  • Can you be more precise as to what syntax you don't understand? There's a variable declaration, two string literals, and 4 method calls in there. Do you know what a method is? Do you know what a method call is? Do you know what a string is? Do you know what a literal is? Do you know what a string literal is? Do you know what a variable is? Commented Sep 15, 2017 at 6:51
  • Jörg - I understand the Java format, but the above syntax didn't make sense as to how it should be comprehended. For e.g. was the get a method, was it being overwritten with the body defined above and what shall the equivalent representation to make it look the traditional way (for e.g. get() {}) . <p> path("hello") {} path("hello") { get {} } path("hello") { get {complete(HttpEntity(,))} }</p> Commented Sep 15, 2017 at 17:22

2 Answers 2

2

These are call-by-name parameters, which are described at various points in the Scala Language Specification. Basically, if you have a declaration like:

def path[R](string: String)(body: => R): R = ... 

you will need to supply a string and a block of code (body), which is a call-by-name block. In this case, if body returns a result of type R, that will be the inferred return type of path. Thus, that method could be called as

path("hello") { "world" } 

The call-by-name block is not invoked until it is used.

Here is a nice explanation by Rob Norris: https://tpolecat.github.io/2014/06/26/call-by-name.html

Sign up to request clarification or add additional context in comments.

Comments

2

As you probably know, it's a route definition in akka http. They are very well described in akka documentation. This particular route works the following way:

  • path("hello"){...} - a directive that verifies the path
  • get{...} - a directive that verifies the http verb (method) So it translates to GET /hello
  • complete(HttpEntity(...)) is a response.

1 Comment

Thanks for answering from the akka interface perspective.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.