Pergunta

I have a class that contain List in scala.
I dont understand the debugger (eclipse) structure.

Why the items are not in the same line ? "X" is because this list is marked as Option[List[String]] .. Where "tl" comes from ??
Why the "value" that actually hold the string list, are not in the same position (brothers).. ?

enter image description here

Json:

{
  "id": "1",
  "references": {
    "configuratorId": "conf id",
    "seekId": "seekid",
    "hsId": "hsid",
    "fpId": "fpid"
  },
  "defaultName": "Product 1 - default name",
  "defaultDescription": "Product 1 - default desc",
  "categories": [
    "string1",
    "string2"
  ]
}

Class:

case class MiniProduct(id: Option[String], defaultName: String, defaultDescription:String, references: References, categories:Option[List[String]])
{}

Class Format:

    implicit val miniProductFormat = new Format[MiniProduct]{
    def writes(item: MiniProduct):JsValue = {
      Json.obj(
          "id" -> item.id,
          "defaultName" -> item.defaultName,
          "defaultDescription" -> item.defaultDescription,
          "references" -> item.references,
          "categories" -> item.categories
          )
    }
    def reads(json: JsValue): JsResult[MiniProduct] = 
    JsSuccess(new MiniProduct(
      (json \ "id").as[Option[String]],
      (json \ "defaultName").as[String],
      (json \ "defaultDescription").as[String],      
      (json \ "references").as[References],
      (json \ "categories").as[Option[List[String]]]
      ))
  }

Controller:

 def addProducts(lang: String, t: String) = Action {
    request =>
      request.body.asJson.map {
        json =>
          val req = json.as[MiniProduct]
          println("Printing list of categories: ")
          req.categories.foreach(println)

And output is OK:

 Printing list of categories:
 List(string1, string2)
Foi útil?

Solução

This is because it is a singly linked list (and as I know it is the default implementation of List() in Scala)

Outras dicas

If you are using the Scala debug launcher, you can enable Show Logical Structure (little tree-like button on the top-right of the Variables view). This will show a list as in the picture below:

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top