Anyone see this useage before?

case class Person(name:String,age:Int){
def doubleAge():Int = {
return 2*age;
}
}

object Person{
implicit val personReads:Reads[Person]=(
(JsPath \ “name”).read[String] and
(JsPath"age").read[Int]
)(Person.apply _)
}

i dont understand how this works,it is the code written in playframework to parse json,im new to scala,anyone can explain why i can write like that

Could you point to the part of the code you don’t understand?

(Note: when you are quoting code here, please put a line containing just three backticks – ``` – before and after the code. That will make it display as code, making it easier to read, and you’re more likely to get answers. Thanks!)

If you don’t already have it to hand, the documentation for this play-json function can be found here.

But I think what you’re asking is “what is this doing?”, and that’s a pretty deep topic. The keyword you are looking for is “typeclass”, and you’ll find a lot talking about them online.

A quick summary is that Reads[T] is a typeclass instance which describes how to read a T from a JSON structure. By marking it as implicit, that means that any code that can see personReads, that wants a Reads[Person], can make use of that. You’ll find that the functions that turn a JsValue into T (such as JsValue.validate()), look for a Reads[T] to tell them how to do it.

Basically, you can think of a typeclass such as Reads[T] as an interface of T. But it’s an interface that is defined outside of T itself, and adds extra functionality to T. A typeclass instance like personReads says how to implement that typeclass for a particular type.

Hope this helps – it’s a deep topic, but a really important one for using Scala well, so it’s worth spending some time learning how it works…