Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `object Foo extends (Bar => Baz)` mean?

Tags:

scala

I was reading dispatch's code, and come across this file, in which, it says:

object Elem extends (Res => scala.xml.Elem) {
  def apply(res: Res) =
    XML.withSAXParser(factory.newSAXParser).loadString(res.getResponseBody)
    ...

What does object Elem extends (Res => scala.xml.Elem) mean?

like image 857
David S. Avatar asked Feb 17 '26 10:02

David S.


1 Answers

A => B is the syntax used to describe anonymous functions.

The object declaration

object Elem extends (Res => scala.xml.Elem) { /* ... */ }

is shorthand for

object Elem extends Function1[Res, scala.xml.Elem] { /* ... */ }

In natural language: Elem is a function which produces a scala.xml.Elem object from a Res object.

A look at the scaladoc for Function1 shows that Function1 declares an abstract apply method which is used to implement the function's logic.

like image 90
Robby Cornelissen Avatar answered Feb 19 '26 03:02

Robby Cornelissen