Question

I'm still trying to get the hang of certain aspects of scala, Play and anorm. Now, Iǘe read all the manuals and online documentation, but I just can seem to find a satisfying solution for my problem.

I have the following code for retreiving an object called MyThing:

case class MyThing(val code: String, val type:String, val value: Double, var id:Long)

object MyThing{

  val myThingConverter = {
    get[String]("type") ~ get[String]("code")~get[Double]("value") ~ get[Long]("id") match {
      case type ~ code ~ value ~ id  =>
        MyThing( "test", " test", value ,  213 )
    }
  }


  val loadQuery =
    """
        "select  * from THINGS where id = {id}"
    """

  def loadThings(id: Long): Option[Thing] = {
    DB.withConnection {
      implicit c =>
        SQL(loadQuery)
          .on('id -> id).as(myThingConvert *)
    }.headOption
}

The problem is that an error is given at the part 'MyThing( "test", " test", value , 213 )' . The error is for "value" :

Type mismatch, expected: Double: actual: Any

What am I doing wrong?

Edit: Added missing parm

Ps.

I have the feeling that pattern matching for extracting values from the resultset of a select query to create objects with, is gross overkill. Is there another way in anorm to accomplish this less painfully?

Was it helpful?

Solution

When I replace "match" with "map" in your converter, it compiles for me:

val myThingConverter = {
  get[String]("type") ~ get[String]("code") ~ getDouble]("value") ~ get[Long]("id") map {
    case tpe ~ code ~ value ~ id  =>
      MyThing( "test", " test", value ,  213 )
  }
}

Also, you can't/shouldn't name variable "type" in Scala.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top