类型类证据参数和具体实例参数之间的类型冲突是否可以解决?

时间:2018-09-07 16:13:36

标签: scala

Is there a way to guarantee case class copy methods exist with type classes in Scala?中,我有一个相关的问题提出了这个新问题。不幸的是,出于晦涩的原因,我需要类型参数。

我的类型类和隐式操作类如下:

  trait Job[J] {
    def id(jb: J): JobId
    def cmd(jb: J): String
    type M <: JobMetaData
    type JRef = J
    def meta(jb: J): M
    def pickler: Pickler[J]
    def rw: RW[J]
    def instance(jb: J, id: JobId, cmd: String, meta: M): J
  }
  object Job{

    type Aux[J0, M0] = Job[J0] {type M = M0}
    implicit class JobDispatch[J](val job: J)(implicit val ev: Job[J]) {
      def id: JobId = ev.id(job)
      def cmd: String = ev.cmd(job)
      def meta: ev.M = ev.meta(job)
      def rw: RW[J] = ev.rw
      def copyJob(
        idIn: JobId = job.id,
        cmdIn: String = job.cmd,
        metaIn: ev.M = job.meta
      ): J = ev.instance(job, idIn, cmdIn, metaIn)
    }

}

值得注意的是instancecopyJob方法; copyJob是公开的API。

现在来看一个具体的例子,我有:

  final case class OneShot(id: JobId, cmd: String, meta: SysJobMetaData) {
    type M = SysJobMetaData
  }

  object OneShot{
    implicit val rw: RW[OneShot] = macroRW
    implicit val jobOneShot: Job.Aux[OneShot, SysJobMetaData] = new Job[OneShot] {
      def id(jb: OneShot): JobId = jb.id
      def cmd(jb: OneShot): String = jb.cmd
      override type M = SysJobMetaData
      def meta(jb: OneShot): M = jb.meta
      val pickler: Pickler[OneShot] = generatePickler
      val rw: RW[OneShot] = OneShot.rw
      def instance(jb: OneShot, id: JobId, cmd: String, meta: M): OneShot =
        jb.copy(id, cmd, meta)
    }

但这会导致错误:

[error] /home/brandon/workspace/CCRS/model/src/main/scala/org/xsede/jobrunner/model/ModelApi.scala:277: type mismatch;
[error]  found   : _1.ev.M where val _1: org.xsede.jobrunner.model.ModelApi.Job.JobDispatch[J]
[error]  required: JobDispatch.this.ev.M
[error]         metaIn: ev.M = job.meta
[error]                            ^

首先,我认为我不完全理解该错误。似乎job.meta应该解析为键入ev.M。这可以解决吗?

编辑1

如果我添加一个asInstanceOf调用以使copyJob定义(= ev.instance(job, idIn, cmdIn, metaIn.asInstanceOf[ev.M]))中的本地事物变得快乐,则事物在本地看起来更快乐,但是类型信息丢失; M <: JobMetaData,但无疑也不会恢复原来的类,包括出现在扩展JobMetaData的类中的成员,例如下面的shell

value shell is not a member of _1.ev.M
[error]     val resultMaybe: Try[CommandResult] = cmd.meta.shell match {
[error]                                                    ^

1 个答案:

答案 0 :(得分:0)

我可能在这里丢失了一些东西,原因是发生了很多事情,但是隐式转换JobDispatch似乎是在递归地调用自身。你为什么不这样做呢?直接调用meta,而不是job.meta

  trait Job[J] {
    def id(jb: J): JobId
    def cmd(jb: J): String
    type M <: JobMetaData
    type JRef = J
    def meta(jb: J): M
    def pickler: Pickler[J]
    def rw: RW[J]
    def instance(jb: J, id: JobId, cmd: String, meta: M): J
  }
  object Job{

    type Aux[J0, M0] = Job[J0] {type M = M0}
    implicit class JobDispatch[J](val job: J)(implicit val ev: Job[J]) {
      def id: JobId = ev.id(job)
      def cmd: String = ev.cmd(job)
      def meta: ev.M = ev.meta(job)
      def rw: RW[J] = ev.rw
      def copyJob(
        idIn: JobId = id,
        cmdIn: String = cmd,
        metaIn: ev.M = meta
      ): J = ev.instance(job, idIn, cmdIn, metaIn)
    }
  }