4

As a bit of background, I'm using joda DateTime from Scala. It seems that everywhere a Scala developer uses Joda DateTime, they define some implicit datetimeOrdering, which they then import. Each library seems to define its own. Occasionally there are conflicts from having multiple definitions from different libraries in scope.

I'm curious if and how the Java library (in this case joda, but it's just an example) might define such an implicit in its Java source code, such that it would compile to equivalent scala bytecode, so that it could be imported in a scala program. I know I can do things like implement def +(x:Whatever):Whatever in Java using $plus(...), but I don't know how I would define an implicit.

I'm not a joda developer, just curious how this would work. Obviously joda could include a single scala file in their project, but I'm curious if other options exist. Does defining an implicit in Java require the scala-tools.jar being on the classpath? Can an implicit be defined directly in Java code? What does the implementation of implicit actually look like?

nairbv
  • 4,045
  • 1
  • 24
  • 26
  • 5
    implicit is used by scalac (**scala** compiler) solely, it used during compilation only and it leaves no traces in resulting bytecode ([unlike invokedynamic](http://stackoverflow.com/a/7134675/298389)). Since mixed java and scala code is compiled in two distinct passes, I think there is no way to define implicit in java code (even, via sophisticated means, like bytecode modification) – om-nom-nom Jun 23 '14 at 16:20
  • 5
    @om-nom-nom It does leave traces, otherwise you couldn't use implicits from libraries you don't have source for. I expect they are in the `@ScalaSignature` annotation. Of course, reproducing a valid `@ScalaSignature` using Java code is not feasible. – Alexey Romanov Jun 23 '14 at 18:08
  • @AlexeyRomanov absolutely valid point, thought about scalasig, but decided to not mention it – om-nom-nom Jun 23 '14 at 18:57

0 Answers0