Amir Afghani
Amir Afghani

Reputation: 38541

Scala function : What is the return type

I'm trying to wrap my head around the following method signature. I believe that logAccess is a function that takes two parameter lists, and returns a function whose return type is a LogEntry.

Or is the return type simply a RouteResult that is lazily evaluated.

def logAccess(remoteAddress: RemoteAddress, start: Long)(request: HttpRequest): RouteResult => Option[LogEntry] = {
   case RouteResult.Complete(result) =>
     val ip = remoteAddress.toOption.map(_.getHostAddress)
     val state = sisCache.get
     statsWriter.recordAccess(state, request.uri.toString, request.method.name, ip, result.status.intValue, time.ms - start)
     Some(LogEntry(s"state: $state, clientIP: $ip, uri: ${request.uri}, method: ${request.method.name}, status: ${result.status}, took: ${time.ms - start}ms", InfoLevel))
   case RouteResult.Rejected(rejections) =>
     None
 }

Upvotes: 2

Views: 73

Answers (1)

Willis Blackburn
Willis Blackburn

Reputation: 8204

logAccess is a function that accepts two parameter lists and returns a (RouteResult => Option[LogEntry]), that is, a function that takes a RouteResult and returns a Option[LogEntry].

The syntax "{ case whatever => ... } defines a partial function, which is an instance of PartialFunction, which is a subclass of Function, as you can see in the documentation. Since this partial function accepts a RouteResult and returns an Option[LogEntry], it is a (RouteResult => Option[LogEntry]) and can be returned from logAccess.

Upvotes: 2

Related Questions