简体   繁体   English

找不到参数c的隐式值:anorm.Column [Float]

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

I got this similar question but it doesn't help me. 我也遇到了类似的问题,但这无济于事。 ( Anorm parse float values ). Anorm解析浮点值 )。

And I can honestly say I didn't understand the solution of that question. 我可以诚实地说,我不理解该问题的解决方案。
I am getting this complie time error: 我收到此complie time错误:

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

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. 您得到的错误只是说anorm.Column[Float]在范围内不是隐式的。 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 . 为您提供更详细的说明:由于Column伴随对象仅为rowToDouble提供隐式, rowToDouble您只需使用问题中链接的代码即可 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. 稍后,您可能希望将其放置在某个封闭范围内的val中。

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

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]. 您正在使用需要参数类型为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). 最新的Anorm(包含在Play 2.3中)提供了更多的数字转换(请参阅http://applicius-en.tumblr.com/post/87829484643/anorm-whats-new-play-2-3和Play迁移说明中的详细信息)。

If you have missing converter, you can add an issue on Play github project. 如果缺少转换器,则可以在Play github项目上添加问题。

Best 最好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM