0

I am new to Scala and implicit conversions.

I am working with an existing scala library over which I have no control. Lets consider a class R from the library.

abstract class R {

}

object R {
    implicit def RFunctions[K, V](r: R[(K, V)]) (implicit kt: ClassTag[K],vt: ClassTag[V], ord: Ordering[K] = null): RFunctions[K, V] = { new RFunctions(r) }
}

I am extending the class R to override almost all of its behavior. But this new class is a Java class. Let's call this A.

class A extends R {

}

Problem is, at some point in my control flow, the control jumps to the implicit declaration for R and ends up using RFunctions class for any functionality.

The methods which are present in RFunctions are also implemented in my extended class A. I do not understand how I can bypass or avoid the implicit conversion.

Any ideas would be appreciated.

beerwin
  • 9,813
  • 6
  • 42
  • 57
  • hmm... `implicit` in Java ? If you are translating it to Java then translate it completely. You will not find an easy way of using implicits in Java. – sarveshseri Dec 16 '16 at 12:06
  • If your class A has all RFunction methods then they will get called before any implicit conversion gets applied – Eduardo Pareja Tobes Dec 16 '16 at 12:35
  • You can't. In some places of the code, especially in the external library, the static type of some variable will be R, even though at runtime it's actually A. Therefore, the implicit conversion will take place anyway, no matter what you do, because the compiler isn't omniscient enough to deduce otherwise. And really, there's no typing relationship between RFunctions and R. You can't "implement" methods of RFunction from a subclass of R, because they are separate entities that just happen to be linked by a converting method. – HTNW Dec 17 '16 at 03:57
  • Also, you have `abstract class R` and then `R[(K, V)]`? – Eduardo Pareja Tobes Dec 17 '16 at 10:47
  • Yes, R[T] and R[Tuple2<>] – HARSH NILESH PATHAK Dec 18 '16 at 02:35

1 Answers1

0

Extend abstract class R with abstract class Rbis in Scala, and in companion object Rbis define the implicit functions you need (they will be called before the ones in R in our case), then in java you extend Rbis.

John K
  • 1,285
  • 6
  • 18