LoginSignup
2
1

More than 5 years have passed since last update.

PlayのReadsの文法的意味(Scala)

Last updated at Posted at 2015-07-02

Playでjson扱うときによく見る下のようなコード。いったい何やってるんだ?

import play.api.libs.json._

class Person(name: String)

implicit val reads = (
  (__ \ 'name).read[String]
)(Person)

Scala初心者の自分にはさっぱりわからず調べたので、メモ書き。

(__ \ 'name)

この、「__」これはplay.api.libs.json.package.scalaに以下のようにJsPathのエイリアスとして定義されてる。

val __ = JsPath

play.api.libs.json._をインポートすることでこのエイリアスがきくようになってるので、以下の二つは同じ意味。

(__ \ 'name)
(JsPath \ 'name)

ってことでこの部分はJsPathの「\」メソッドの呼び出しってことになるので、以下と同じ。

(JsPath \ ('name))

初心者には何が書いてあるのか理解しにくいコード。
サンプル見れば使うことはできるんだけど、文法理解できたほうが楽しい。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1