Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:58:17+00:00 2026-06-03T18:58:17+00:00

I have been working on an issue with implicit conversion for days now, but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:58:20+00:00Added an answer on June 3, 2026 at 6:58 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this issue for a few days now and can't
I have been working on this problem for 2 days now and it's an
I have been working on this problem for close to 4 days now. I
I have been working on this frustrating bug for a few days now and
I have been working on the same task since 2 days, but no result
I have been working on a json decode issue (which I have already had
I have been working on a Windows Mobile app for a little while now
I have been working on a project for a few months now and my
I have been working on a project for a long time now and there
I have been working on Visual Studio 2005 since last couple of days. Till

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.