Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find what is preventing an implicit conversion (pimp class)?

Tags:

scala

implicit

I have a code which uses pimped jme Quaternion math. The math extension (for Java jMonkeyEngine Quaternion) looks like this:

object Operators {
  implicit class QuaternionMath(val a: Quaternion) extends AnyVal {
    def * (b: Quaternion) = a mult b
    //def * (b: Float) = a mult b
  }
}

The code using the math looks like this:

import Operators._

... and later in a function:

def compute(q1: Quaternion, q2: Quaternion) = {
  q1 * q2
}

Now after adding some more code which seems not related, I get a syntax error:

Error:(477, 10) value * is not a member of com.jme3.math.Quaternion

To fix the error I need to repeat the import right before the computation, either as a wildcard again, or a specific one:

import Operators.QuaternionMath

import Operators._

I have no idea what can be hiding the QuaternionMath implicit conversion. Is there some way how to check that, some tool, or some compiler switches?

like image 349
Suma Avatar asked Dec 06 '25 03:12

Suma


2 Answers

If QuaternionMath really is in scope, you should be able to write

val m: location.of.Operators.QuaternionMath = QuaternionMath(q1)
m.*(c2)

If the first line fails, it will be telling either that

  • value QuaternionMath is not found. It's really not in scope
  • that you can't call it that way, and then you have shadowed it by something else called QuaternionMath. In this case you could in an IDE just jump to the source of that shadowing symbol.
  • that QuaternionMath is not a member of Operators (my guess here). Then Operators is not that object that you imagine but another one with the same name. Again in the IDE, just select the Operators bit of import Operators._ and look up the source (Ctrl-B in IntelliJ) to verify you are doing what you think you are doing.
like image 166
0__ Avatar answered Dec 09 '25 15:12

0__


If you are using IntelliJ IDEA for Scala then try checking possible implicit conversions by selecting an expression and pressing ctrl+shift+q (ctrl+q for Mac), check this tutorial.

For Scala IDE there is implicit conversion highlighting.

like image 40
Przemek Avatar answered Dec 09 '25 16:12

Przemek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!