1

I'm pretty new to Scala and came from Java and got confused by some piece of code when reading this documentation. Here is the code.

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

Where path("hello") is the method of trait:

trait PathDirectives /*extends omitted*/ {

    def path[L](pm: PathMatcher[L]): Directive[L] = pathPrefix(pm ~ PathEnd)
   // the rest omitted
}

So, when we invoke the path("hello") method we would need an object implementing the trait to invoke it on. But in the example it was just a method invocation. Just like a static method.

What did I miss?

1 Answer 1

4

So, when we invoke the path("hello") method we would need an object implementing the trait to invoke it on.

Yes, and that object is akka.http.scaladsl.server.Directives. The reason that you don't need to write Directives.path is that the code imports Directives._, so you can call Directives' methods directly (similar to a static import in Java except the method doesn't have to be static).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.