Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to make the conversion available <em>within the trait</em>. You can't pass it in from the outside implicitly because the outside doesn't know that <code>persist</code> secretly requires <code>realPersist</code> which requires an implicit conversion. This all fails even without considering <code>JPersistable</code>.</p> <p>You can for example add</p> <pre><code>implicit def view: DTOType =&gt; EntityType </code></pre> <p>as a method in the trait and it will then compile. (You can drop <code>realPersist</code> then also.)</p> <p>Then you need a way to get that view set. You can</p> <pre><code>case class Persister()(implicit val view: B =&gt; D) extends Persistable[B,D] </code></pre> <p>and then you're all good. (The <code>implicit val</code> satisfies the <code>implicit def</code> of the trait.)</p> <p>But now you have bigger problems: your Java interface signature <em>doesn't match your Scala signature</em>. The equivalent Scala is</p> <pre><code>trait JPersistable[T &lt;: Object] { def persist(t: T): T } </code></pre> <p>See how <code>persist</code> takes and returns the same type? And see how it <em>does not</em> 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 <code>persist</code> that maps from <code>DTOType</code> to <code>EntityType</code>, but you really just have the <code>EntityType</code> to <code>EntityType</code> transform that the Java interface requires.</p> <hr> <p>Edit: for example, here's a working version of what you posted just using standard implicit conversion:</p> <pre><code>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 &lt;: 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()) } </code></pre> <p>Pay attention to which types are used where! (Who takes <code>B</code> and who takes <code>D</code> and which direction do you need a conversion?)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload