I have an implicit class defined in a class injected in other classes like this
class A {
implicit class B(s: String) {
def b = ???
}
}
class C(a: A) {}
Is there a way to access implicit class B (and in particular its method b) from the class C, without importing it explicitly? (Please note that class A could not be a trait since it also injects some classes.)
Solution 1 (import
a._)Well, yes, as already noted in the comments, from your requirements it is not obvious why you wouldn't just import
a._in the body ofC:This one line really doesn't hurt anyone.
If you still don't like it, then the problem might be elsewhere. Thus my second proposal.
Solution 2 (separating typeclass-like
A-interface from.b-syntax)This other solution is less about the reduction of number of
imports in your code, and it doesn't even keep classBinsideA. But it might address another issue that you maybe just can't quite articulate: it separates the functionality provided byAfrom the syntax provided byB.The structure of the following snippet is inspired by the design of the Scala Cats library, that follows a very clear policy with the implicit declarations, always separating the typeclass defintions from the syntax.
The main idea is that:
AIntfprovide actual functionalityBprovides only some additional "pimp-my-library"-style methodsand that we want to keep these two things separate.
Here is how to separate them, thereby also avoiding
import a._inside ofC. First, you define the interface that describes the functionality provided byA:Then you can implement it by a few different A`s:
Note that the object
Bhas disappeared fromA. Instead, it is moved in a separatesyntax-package, and renamed toA_Ops. The methodbis also renamed toa:This is how you use it:
A_Intfsyntax.a._a-argument ofCas implicit"string".asyntax insideCwithout further imports.In code:
Now the implementations of
AIntfand the syntax.abecome independent. You can injectA2instead ofA. Or you can change the syntax from"str".ato"str".somethingEntirelyDifferent.The full code snippet:
Unfortunately, I have no clue what
guiceis going to do with an implicit argument, have not tried it yet. It might force you to writewhich then becomes longer then the simple
importmentioned in the first part.