St.Antario St.Antario - 1 year ago 86
Scala Question

Understanding method invocation in Scala

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?

Answer Source

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).