Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make this Scala implicit conversion work?
    text
    copied!<p>I'm using a library that gives me the following (relations can be implicitly converted to nodes):</p> <pre><code>class Relation[A,B] class Node[A,B](r: Relation[A,B]) implicit def relation2node[A,B](r: Relation[A,B]) = new Node(r) </code></pre> <p>I'm extending Relation for my own use:</p> <pre><code>class XRelation[A] extends Relation[A,Int] </code></pre> <p>Relations/XRelations are meant to be subclassed:</p> <pre><code>class User extends XRelation[Int] </code></pre> <p>Now, I also define my own Helper methods like GET, designed to work with any Node and anything that converts to Node:</p> <pre><code>class Helper[A,B](n: Node[A,B]) { def GET {} } // note: this is the only way I know of to make the next example work. implicit def xx2node2helper[A,B,C[_,_]](x: C[A,B])(implicit f: C[A,B] =&gt; Node[A,B]) = new Helper(x) </code></pre> <p>So this example works:</p> <pre><code>new Relation[Int,Int]().GET </code></pre> <p>And if I add another implicit conversion:</p> <pre><code>// don't understand why this doesn't work for the previous example // but works for the next example implicit def x2node2helper[A,B,C](x: C)(implicit f: C =&gt; Node[A,B]) = new Helper(x) </code></pre> <p>I can also make the following conversion work:</p> <pre><code>new XRelation[Int]().GET </code></pre> <p>But this doesn't work:</p> <pre><code>new User().GET </code></pre> <p>Sadly, that fails with:</p> <pre><code>error: No implicit view available from Sandbox3.User =&gt; Sandbox3.Node[A,B] </code></pre> <p>Can anyone make sense of all this and explain how to get the last example to work? Thanks in advance.</p> <p><strong>Update</strong>: I know you can just introduce implicit conversions from <code>Relation</code>, but I'm asking to (1) figure out how to do this without having to introduce implicits from every single type that could possibly implicitly convert to Node, and (2) to solidify my understanding of implicits.</p>
 

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