Jon Burgin
Jon Burgin

Reputation: 83

scala list type mismatch

So I had list of tuples like this:

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)})

And there was much happiness in the land, but when I changed list of room like so:

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)})

I get:

[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)

So it appears that the list is now of Object instead of JField as it was before, why is that?

Upvotes: 1

Views: 1180

Answers (1)

dhg
dhg

Reputation: 52701

It works if you don't detach the List from its (:

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

Basically, it's parsing like this:

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

The error comes because the JObject constructor expects a JValue but you are passing it fields which has type List.type.

Upvotes: 2

Related Questions