1

I found a great explanation of Reads[T] here: Syntax and meaning of a Scala/Play! code sample.

One thing I don't fully understand is why Format[T] does not require the underscore for Creature.apply here:

import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val creatureFormat = (
  (__ \ "name").format[String] and
  (__ \ "isDead").format[Boolean] and
  (__ \ "weight").format[Float]
)(Creature.apply, unlift(Creature.unapply))  

but if this was just Reads[T] the example says

import play.api.libs.json._
import play.api.libs.functional.syntax._

implicit val creatureReads = (
   (__ \ "name").read[String] and
   (__ \ "isDead").read[Boolean] and
   (__ \ "weight").read[Float]
 )(Creature.apply _)

Does Reads not 'require' _ ? In my code using a mix of Reads[T] and Format[T] based on them model and it got me thinking why the difference.

Community
  • 1
  • 1
Barry
  • 1,800
  • 2
  • 25
  • 46

1 Answers1

1

_ following a method indicates partial application.

This is needed when you need to transform a method into a function.

Most of the times you can skip the _ as the compiler is able to infer this from the context and automatically expand the method into a function.

So you can drop the _ in both cases.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235