Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to make implicit conversion of types used in my Interpreter
    primarykey
    data
    text
    <p>I am writing an interpreter and tried to use solution from <a href="https://stackoverflow.com/questions/3088979/how-to-set-up-implicit-conversion-to-allow-arithmetic-between-numeric-types/3102091">how-to-set-up-implicit-conversion-to-allow-arithmetic-between-numeric-types</a> for the same problem I need to be able to add Boolean + Boolean, Int + Boolean, Boolean + Int, Int + Double, Double + Double etc.</p> <p>So I used WeakConformance and C classes from that solution</p> <pre><code>sealed trait WeakConformance[A &lt;: AnyVal, B &lt;: AnyVal, C] { implicit def aToC(a: A): C implicit def bToC(b: B): C } object WeakConformance { implicit def SameSame[T &lt;: AnyVal]: WeakConformance[T, T, T] = new WeakConformance[T, T, T] { implicit def aToC(a: T): T = a implicit def bToC(b: T): T = b } implicit def IntDouble: WeakConformance[Int, Double, Double] = new WeakConformance[Int, Double, Double] { implicit def aToC(a: Int) = a implicit def bToC(b: Double) = b } implicit def DoubleInt: WeakConformance[Double, Int, Double] = new WeakConformance[Double, Int, Double] { implicit def aToC(a: Double) = a implicit def bToC(b: Int) = b } } case class C[A &lt;: AnyVal](val value:A) { import WeakConformance.unify def +[B &lt;: AnyVal, WeakLub &lt;: AnyVal](that:C[B])(implicit wc: WeakConformance[A, B, WeakLub], num: Numeric[WeakLub]): C[WeakLub] = { new C[WeakLub](num.plus(wc.aToC(x), wc.bToC(y))) } } </code></pre> <p>and here is part of my interpreter</p> <pre><code>class Interpreter { ...... def eval(e: Expression): Any = e match { ... case ADD(lhs, rhs) =&gt; (eval(lhs), eval(rhs)) match { case (l: C[_], r: C[_]) =&gt; l + r // error comes here case _ =&gt; error("...") } } } </code></pre> <p>the error is like that</p> <p>error: ambiguous implicit values: // shows 2 last objects declared as implicit in <code>Numeric</code> trait here match expected type <code>Numeric[WeakLub]</code></p> <p>any ideas how to make it work? I wanted to make the eval method to return <code>C</code> but since <code>C[Int]</code> is not an instance of <code>C[Any]</code> it doesn't solve my problem</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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