Json.obj Scala, string concat: Compilation error

Viewed 455

I'm trying to do the next in Scala, I'm using play2:

val str = "another"
val r = Json.obj("error_type" -> "invalid_request_error",
            "validation_errors" -> (Json.obj(
              "code" -> "this mode " + str + " does not exist",
              "param" -> "mode"
            )))

but it gives to me the error:

Type mismatch, expected: (String, Json.JsValueWrapper), actual: String

but if I do:

val r = Json.obj("error_type" -> "invalid_request_error",
            "validation_errors" -> (Json.obj(
              ("this mode ".+(str)).+(" does not exist"),
              "param" -> "mode"
            ))))

It compile and works...

How can I write it in the form str1 + str2 + str3 more readable? How is the order/precedence related here? In my answer I don't understand why is the () needed neither the comment. Is there another similar case when parenthesis are needed?

ps: I'm not sure if in Java is the same issue

3 Answers
Related