Search code examples
scalashapelesslenses

Shapeless: lens compiled error on class type parameter


I defined a base trait for all case class, and intend to change the fields(e.g. createTime, updateTime) in base trait by shapeless lens.

Base on the following codes.

// LensTest.scala

package demo

import java.sql.Timestamp

import shapeless._

trait BaseEntity {
  def id: Int
  def createTime: Timestamp
  def updateTime: Timestamp
}

case class User(id: Int, name: String, age: Int, createTime: Timestamp, updateTime: Timestamp) extends BaseEntity

object Util {
  val witCreateTime = Witness('createTime)
  type tpeCreateTime = witCreateTime.T

  def updateCreateTime[A](a: A, time: Timestamp)(implicit mkLens: MkFieldLens.Aux[A, tpeCreateTime, Timestamp]): A = {
    val lenCreateTime = mkLens()
    lenCreateTime.set(a)(time)
  }
}

/**
  * LensTest
  *
  */
object LensTest extends App {
  val user = User(1, "Tom", 18, null, null)
  val now = new Timestamp(System.currentTimeMillis())

  def normal_method_call_is_ok(): Unit = {
    println(Util.updateCreateTime(user, now))
  }
  normal_method_call_is_ok()

  // However, in the below `BaseRepo` definition,
  // Compiler gives an compile error:
  //
  //     could not find implicit value for parameter mkLens: shapeless.MkFieldLens.Aux[E,demo.Util.tpeCreateTime,java.sql.Timestamp]
  //
  //
  // Why? And how to fixed it?
}


class BaseRepo[E <: BaseEntity] {
  def beforeUpdate(e: E): E = {
    val now = new Timestamp(System.currentTimeMillis())
    Util.updateCreateTime(e, now)
  }
}

As I was commented in the code. I wrote a updateCreateTime function to achieve my goal. It works when I call it in a normal function. However, when I put it in BaseRepo class (which has type parameters), it compile failed. I don't know why this happened, and how to fix it? Any help would be thankful.


Solution

  • Give the compiler what it needs:

    class BaseRepo[E <: BaseEntity] {
      def beforeUpdate(e: E)(implicit in: MkFieldLens.Aux[E, tpeCreateTime, Timestamp]): E = {
        val now = new Timestamp(System.currentTimeMillis())
        Util.updateCreateTime(e, now)
      }
    }
    
    object LensTest extends App {
      val user = User(1, "Tom", 18, null, null)
      val now = new Timestamp(System.currentTimeMillis())
    
      def normal_method_call_is_ok(): Unit = {
        println(Util.updateCreateTime(user, now))
      }
      //normal_method_call_is_ok()
    
      val _user = new BaseRepo[User]
      println(_user.beforeUpdate(user))
      // However, in the below `BaseRepo` definition,
      // Compiler gives an compile error:
      //
      //     could not find implicit value for parameter mkLens: shapeless.MkFieldLens.Aux[E,demo.Util.tpeCreateTime,java.sql.Timestamp]
      //
      //
      // Why? And how to fixed it?
    }
    

    Gives:

    User(1,Tom,18,2018-05-10 11:39:50.287,null)