LeChe
LeChe

Reputation: 1286

Scala implicit conversion on generic trait implementing Java interface

I have been working on an issue with implicit conversion for days now, but somehow I just cannot figure out what I am doing wrong. I read through all the other questions on SO that deal with implicits but I still don't understand what the problem is.

As an example, let's consider a Java interface like this(T extends Object for brevity):

public interface JPersistable<T extends Object> {
    public T persist(T entity);
}

In scala, I do the following:

case class A()
case class B() extends A
case class C()
case class D() extends C

trait Persistable[DTOType <: A, EntityType <: C] {
  // this would be implemented somewhere else
  private def doPersist(source: EntityType): EntityType = source

  // this does not implement the method from the Java interface
  private def realPersist(source: DTOType)(implicit view: DTOType => EntityType): EntityType = doPersist(source)

  // this DOES implement the method from the Java interface, however it throws:
  // error: No implicit view available from DTOType => EntityType.
  def persist(source: DTOType): EntityType = realPersist(source)
}

case class Persister() extends Persistable[B, D] with JPersistable[B]

object Mappings {
  implicit def BToD(source: B): D = D()
}

object Test {
  def main(args: Array[String]) {

    import Mappings._
    val persisted = Persister().persist(B())
  }
}

As stated in the comment, I get an exception at compile time. I guess my questions are:

1) Why do I need to specify the implicit conversion on the doRealPersist explicitly? I expected the conversion to happen even if I do the following:

trait Persistable[DTOType <: A, EntityType <: C] {
  // this would be implemented somewhere else
  private def doPersist(source: EntityType): EntityType = source

  def persist(source: DTOType): EntityType = doPersist(source)
}

However, this does not compile either.

2) Why does compilation fail at persist and not at the actual method call (val persisted = Persister().persist(B()))? That should be the first place where the actual type of EntityType and DTOType are known, right?

3) Is there a better way to do what I am trying to achieve? Again, this is not the actual thing I am trying to do, but close enough.

Apologies in advance if this question is ignorant and thanks a lot in advance for your help.

Upvotes: 1

Views: 1266

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167911

You need to make the conversion available within the trait. You can't pass it in from the outside implicitly because the outside doesn't know that persist secretly requires realPersist which requires an implicit conversion. This all fails even without considering JPersistable.

You can for example add

implicit def view: DTOType => EntityType

as a method in the trait and it will then compile. (You can drop realPersist then also.)

Then you need a way to get that view set. You can

case class Persister()(implicit val view: B => D) extends Persistable[B,D]

and then you're all good. (The implicit val satisfies the implicit def of the trait.)

But now you have bigger problems: your Java interface signature doesn't match your Scala signature. The equivalent Scala is

trait JPersistable[T <: Object] { def persist(t: T): T }

See how persist takes and returns the same type? And see how it does not in your Scala class? That's not going to work, nor should it! So you have to rethink exactly what you're trying to accomplish here. Maybe you just want to make the implicit conversion available--not pass it to the method!--and have Scala apply the implicit conversion for you so that you think you've got a persist that maps from DTOType to EntityType, but you really just have the EntityType to EntityType transform that the Java interface requires.


Edit: for example, here's a working version of what you posted just using standard implicit conversion:

trait JPer[T] { def persist(t: T): T }
class A
case class B() extends A
class C
case class D() extends C
trait Per[Y <: C] extends JPer[Y] {
  private def doIt(y: Y): Y = y
  def persist(y: Y) = doIt(y)
}
case class Perer() extends Per[D] // "with JPer" wouldn't add anything!
object Maps { implicit def BtoD(b: B): D = D() }
object Test extends App {
  import Maps._
  val persisted = Perer().persist(B())
}

Pay attention to which types are used where! (Who takes B and who takes D and which direction do you need a conversion?)

Upvotes: 2

Related Questions