Question

I got this similar question but it doesn't help me. (Anorm parse float values).

And I can honestly say I didn't understand the solution of that question.
I am getting this complie time error:

could not find implicit value for parameter c: anorm.Column[Float]

at

def getInformation(id: Long): List[(Float, Float, Float)] = {
    DB.withConnection { implicit con =>
      val query = SQL("select principal,interest,value from myTable where userId={id} and status=true").on("id"->id)
      val result = query().map { row =>
        Tuple3(row[Float]("principal"), row[Float]("inetrest"), row[Float]("value"))
       //      ^
      }.toList
      return result
    }
  }
Was it helpful?

Solution 2

try this...

def getInformation(id: Long): List[(Float, Float, Float)] = {
    DB.withConnection { implicit con =>
      val query = SQL("select principal,interest,value from myTable where userId={id} and status=true").on("id"->id)
      val result = query().map { row =>
        Tuple3(row[Float]("principal").asInstanceOf[Float], row[Float]("inetrest").asInstanceOf[Float], row[Float]("value").asInstanceOf[Float])
      }.toList
      return result
    }
  }

implicit def rowToFloat: Column[Float] = Column.nonNull { (value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
  case d: Float => Right(d)
  case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to Float for column " + qualified))
}
}

OTHER TIPS

Maybe a short review of implicits help you. Let's construct a very basic example:

// some class which will be used as implicit (can be anything)
case class SomeImplicitInformation(maybe: Int, with: Int, data: Int)

// lets assume we have a function that requires an implicit
def functionRequiringImplicit(regularParameters: Int)(implicit imp: SomeImplicitInformation) {
  // ...
}

// now if you try to call the function without having an implicit in scope
// you would have to pass it explicitly as second parameter list:
functionRequiringImplicit(0)(SomeImplicitInformation(0,0,0))

// instead you can declare an implicit somewhere in your scope:
implicit val imp = SomeImplicitInformation(0,0,0)

// and now you can call:
functionRequiringImplicit(0)

The error you get simply says that anorm.Column[Float] in not in the scope as implicit. You can solve it by adding it implicitly to your scope or pass it explicitly.

More detailed instructions for you: Since the Column companion object only provides an implicit for rowToDouble you simply have to use the code that is linked in your question. To get it to work put it before your result computation. Later you might want to place it in a val in some enclosing scope.

Some functions can accept what we call implicit parameters. Such parameters can, under certain conditions, be derived from the context. If these parameters can't be found, then you have to specify them by hand. If you expect a parameter to be used as an implicit one, it must have been declared implicit, for instance this way :

implicit val myVal = ...

It can be done in the current block or in an enclosing one (in the class body, for instance, or even sometimes in the imports)

The error you get seems to be related to this feature. You're using a function that needs a parameter of type anorm.Column[Float]. The argument is defined to be implicit so that an implicit value can be used and your code may be more concise. Unfortunately, you don't seem to have such an implicit value in your code, so it fails.

Latest Anorm (included in Play 2.3) provides more numeric conversion (see details at http://applicius-en.tumblr.com/post/87829484643/anorm-whats-new-play-2-3 & in Play migration notes).

If you have missing converter, you can add an issue on Play github project.

Best

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