1

所以我有这样的元组列表:

val rooms = List(("Hi", "mom"),("hi", "dad"))
val foo = rooms.map(arg =>{
                  var fields = List
                    ( new JField("greeting",arg._1),
                      new JField("recipient",arg._2))
                      new JObject(fields)})

这片土地上有很多幸福,但是当我像这样更改房间列表时:

case class Room(greeting:String, recipient:String)
val rooms = List(Room("Hi", "mom"),Room("hi", "dad"))
val foo = rooms.map(arg =>{
                  var fields = List
                    ( new JField("greeting",arg.greeting),
                      new JField("recipient",arg.recipient))
                      new JObject(fields)})

我得到:

[error] <file>: type mismatch;
[error]  found   : scala.collection.immutable.List.type (with underlying type object List)
[error]  required: List[blueeyes.json.JsonAST.JValue]
[error]                       new JArray(fields)

所以现在列表似乎是 Object 而不是 JField ,这是为什么呢?

4

1 回答 1

2

如果您不将其与其分离,它会List起作用(

var fields = List(
  new JField("greeting", arg.greeting),
  new JField("recipient", arg.recipient))

基本上,它是这样解析的:

var fields = List                         // assign the List companion object

(new JField("greeting", arg.greeting),    // construct a tuple with two items
  new JField("recipient", arg.recipient)) //   ...but don't use or assign it

new JObject(fields)                       // Make JObject containing the type

错误的出现是因为JObject构造函数需要 aJValue但您传递的是它fields的 type List.type

于 2012-05-20T21:55:55.563 回答