I was wondering how to place effectively the same implicit conversion in more than one package in such a way that importing the contents of one is enough, but importing all doesn't introduce conflicts. Regardless of what's your opinion about the idea as stated (I can guess), it's almost possible:
trait syntax
object syntax {
implicit class syntaxIntExtension(a :Int) {
def times2 = a * 2
def times3 = a * 3
}
}
trait extension extends syntax
object extension {
implicit class dialectIntExtension(a :Int) extends syntax.syntaxIntExtension(a) {
override def times2 = a + a
def times4 = a.times2.times2
}
}
object lazyUser {
import syntax._
import extension._
1.times2
1.times3
1.times4
}
The caveat here is that the scopes syntax and extension must be objects; package objects won't work, and I have absolutely no idea what I could even try in Scala 3. Can I somehow use syntax and expansion identifiers as normal scopes, containing normal 'non inner' classes? The only ways I can think of is
- to have
syntaximplandextensionimpl, and fillsyntaxandextensionup to the brim with type aliases andvals. Me not like. - declare everything I want as inner types of some wrapper
traits and composesyntaxandextensionby mixing in a long list of such wrappers. Ugh.
Now, I can make it work relying solely on the other implicit precedence rule of being 'more specific than', but it comes with some drawbacks, such as the need for Scala 2 style pseudo extension methods even in Scala 3. I wonder thus if there is something I haven't thought about when trying to exploit the former rule.