简体   繁体   English

无法找到类型证据参数的隐式值

[英]Could not find implicit value for evidence parameter of type

I am trying to write a Mapper that would convert Option[sql.Date] to Option[sql.Timestamp].我正在尝试编写一个映射器,将 Option[sql.Date] 转换为 Option[sql.Timestamp]。 For this, I am using the following code:为此,我使用以下代码:

package model.db

import java.sql.Date

import com.typesafe.slick.driver.ms.SQLServerDriver.simple._

abstract class RichTable[T](tag: Tag, name: String, schema: Option[String] = Some("dbo")) extends Table[T](tag, schema, name) {
  def id: Column[Option[Long]] = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc)
  def cancelled: Column[Option[Date]] = column[Option[Date]]("cancelled")(DateMapper.sqlDate2SqlTimestampOptionMapper)
}

abstract class LoggableTable[T](tag: Tag, name: String, schema: Option[String] = Some("dbo")) extends RichTable[T](tag, name, schema) {
  def createdAt: Column[Date] = column[Date]("created_at", O.NotNull)(DateMapper.sqlDate2SqlTimestampMapper)
}

object DateMapper {
  val sqlDate2SqlTimestampMapper = MappedColumnType.base[java.sql.Date, java.sql.Timestamp](
    { sqlDate => new java.sql.Timestamp(sqlDate.getTime) },
    { sqlTimestamp => new java.sql.Date(sqlTimestamp.getTime) })

  val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option[java.sql.Date], scala.Option[java.sql.Timestamp]](
    { sqlDate => scala.Some(new java.sql.Timestamp(sqlDate.get.getTime)) },
    { sqlTimestamp => scala.Some(new java.sql.Date(sqlTimestamp.get.getTime)) })
} 

The sqlDate2SqlTimestampMapper works fine, so I used it as a base code for the sqlDate2SqlTimestampOptionMapper . sqlDate2SqlTimestampMapper工作正常,所以我用它作为sqlDate2SqlTimestampOptionMapper的基本代码。

When I try to compile, I get the following error:当我尝试编译时,出现以下错误:

Error:(21, 126) could not find implicit value for evidence parameter of type com.typesafe.slick.driver.ms.SQLServerDriver.BaseColumnType[Option[java.sql.Timestamp]] val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option[java.sql.Date], scala.Option[java.sql.Timestamp]](错误:(21, 126) 找不到 com.typesafe.slick.driver.ms.SQLServerDriver.BaseColumnType[Option[java.sql.Timestamp]] val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option 类型的证据参数的隐式值[java.sql.Date], scala.Option[java.sql.Timestamp]](

Error:(21, 126) not enough arguments for method base: (implicit evidence$5: scala.reflect.ClassTag[Option[java.sql.Date]], implicit evidence$6: com.typesafe.slick.driver.ms.SQLServerDriver.BaseColumnType[Option[java.sql.Timestamp]])com.typesafe.slick.driver.ms.SQLServerDriver.BaseColumnType[Option[java.sql.Date]].错误:(21, 126) 方法库的参数不足: (隐式证据$5: scala.reflect.ClassTag[Option[java.sql.Date]], 隐式证据$6: com.typesafe.slick.driver.ms.SQLServerDriver .BaseColumnType[Option[java.sql.Timestamp]])com.typesafe.slick.driver.ms.SQLServerDriver.BaseColumnType[Option[java.sql.Date]]。 Unspecified value parameter evidence$6.未指定值参数证据$6。 val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option[java.sql.Date], scala.Option[java.sql.Timestamp]]( val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option[java.sql.Date], scala.Option[java.sql.Timestamp]](

How can I fix this?我怎样才能解决这个问题?

Thank you in advance.先感谢您。

You do not need option mapper.您不需要选项映射器。 Remove this piece of code.删除这段代码。

val sqlDate2SqlTimestampOptionMapper = MappedColumnType.base[scala.Option[java.sql.Date], scala.Option[java.sql.Timestamp]](
    { sqlDate => scala.Some(new java.sql.Timestamp(sqlDate.get.getTime)) },
    { sqlTimestamp => scala.Some(new java.sql.Date(sqlTimestamp.get.getTime)) })

Now your date mapper becomes现在你的日期映射器变成

object DateMapper {
  val sqlDate2SqlTimestampMapper = MappedColumnType.base[java.sql.Date, java.sql.Timestamp](
    { sqlDate => new java.sql.Timestamp(sqlDate.getTime) },
    { sqlTimestamp => new java.sql.Date(sqlTimestamp.getTime) })
}

In Slick if you give mapping for some T then you need not implement mapping for Option[T]在 Slick 中,如果您为某个T提供映射,则无需为Option[T]实现映射

Just like @pamu said, there is no need for a mapper for an Option.正如@pamu 所说,Option 不需要映射器。 The stacktrace was complaining for a implicit value;堆栈跟踪抱怨隐含值; so what I did was make the mapper implict, and the problem was solved.所以我所做的是让映射器隐含,问题就解决了。

package model.db

import java.sql.Date

import com.typesafe.slick.driver.ms.SQLServerDriver.simple._

abstract class RichTable[T](tag: Tag, name: String, schema: Option[String] = Some("dbo")) extends Table[T](tag, schema, name) {
  def id: Column[Option[Long]] = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc)
  def cancelled: Column[Option[Date]] = column[Option[Date]]("cancelled")
}

abstract class LoggableTable[T](tag: Tag, name: String, schema: Option[String] = Some("dbo")) extends RichTable[T](tag, name, schema) {
  def createdAt: Column[Date] = column[Date]("created_at", O.NotNull)
}

object DateMapper {
  implicit val sqlDate2SqlTimestampMapper = MappedColumnType.base[java.sql.Date, java.sql.Timestamp](
    { sqlDate => new java.sql.Timestamp(sqlDate.getTime) },
    { sqlTimestamp => new java.sql.Date(sqlTimestamp.getTime) })
}

暂无
暂无

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

相关问题 找不到类型^的证据参数的隐含值 - could not find implicit value for evidence parameter of type ^ 找不到类型为证据的隐式值-Spark - could not find implicit value for evidence parameter of type - Spark 使用 scalamock:找不到类型错误的证据参数的隐式值 - Using scalamock: Could not find implicit value for evidence parameter of type error 无法找到有序类型的证据参数的隐含值[T] - could not find implicit value for evidence parameter of type Ordered[T] Scala编译错误-找不到类型为证据的隐式值 - Scala compile error - could not find implicit value for evidence parameter of type 找不到scalaz.Applicative类型的证据参数的隐含值 - could not find implicit value for evidence parameter of type scalaz.Applicative 找不到隐式json格式的证据参数的隐式值 - could not find implicit value for evidence parameter for implicit json formats 找不到scala.reflect.ClassManifest [T]类型的证据参数的隐式值 - Could not find implicit value for evidence parameter of type scala.reflect.ClassManifest[T] 找不到JsonSupport.this.JF类型的证据参数的隐式值[org.joda.time.LocalDateTime] - could not find implicit value for evidence parameter of type JsonSupport.this.JF[org.joda.time.LocalDateTime] 斯卡拉<console> :24: 错误: 找不到微风.storage.DefaultArrayValue [Any] 类型的证据参数的隐式值 - Scala <console>:24: error: could not find implicit value for evidence parameter of type breeze.storage.DefaultArrayValue[Any]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM