In Scala 2 you can use the trick with hiding implicit by name
// Scala 2
trait FooBar[X] {
def value: String
}
object FooBar {
implicit val intIsFooBar: FooBar[Int] = new FooBar[Int] {
override val value: String = "a"
}
}
trait Intf {
type A
implicit def aIsFoobar: FooBar[A]
}
object IntImpl extends Intf {
override type A = Int
override implicit val aIsFoobar: FooBar[A] = {
lazy val aIsFoobar = ???
implicitly[FooBar[A]]
}
}
println(IntImpl.aIsFoobar.value) // a
NullPointerException on implicit resolution
In Scala 3, what's the canonical method for pattern match that uses an erased type?
Is there a workaround for this format parameter in Scala?
Extending an object with a trait which needs implicit member
Constructing an overridable implicit (answer)
In Scala 3 this trick doesn't work any more.
In Scala 3 you can try to make the method inline and use scala.compiletime.summonInline
rather than the ordinary summon
// Scala 3
trait FooBar[X]:
def value: String
object FooBar:
given intIsFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "a"
trait Intf:
type A
/*inline*/ given aIsFoobar: FooBar[A]
object IntImpl extends Intf:
override type A = Int
override inline given aIsFoobar: FooBar[A] = summonInline[FooBar[A]]
println(IntImpl.aIsFoobar.value) // a
Overriding inline methods: https://docs.scala-lang.org/scala3/reference/metaprogramming/inline.html#rules-for-overriding
Please notice that with inlining we modified the method semantics. The implicit is resolved at the call site, not at the definition site
// Scala 2
trait FooBar[X] {
def value: String
}
object FooBar {
implicit val intIsFooBar: FooBar[Int] = new FooBar[Int] {
override val value: String = "a"
}
}
trait Intf {
type A
implicit def aIsFoobar: FooBar[A]
}
object IntImpl extends Intf {
override type A = Int
override implicit val aIsFoobar: FooBar[A] = {
lazy val aIsFoobar = ???
implicitly[FooBar[A]]
}
}
{
implicit val anotherIntFooBar: FooBar[Int] = new FooBar[Int] {
override val value: String = "b"
}
println(IntImpl.aIsFoobar.value) // a
}
// Scala 3
trait FooBar[X]:
def value: String
object FooBar:
given intIsFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "a"
trait Intf:
type A
/*inline*/ given aIsFoobar: FooBar[A]
object IntImpl extends Intf:
override type A = Int
override inline given aIsFoobar: FooBar[A] = summonInline[FooBar[A]]
{
given anotherIntFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "b"
println(IntImpl.aIsFoobar.value) // b
}
About the difference implicitly
vs. implicit
:
When doing implicit resolution with type parameters, why does val placement matter?
Why the Scala compiler can provide implicit outside of object, but cannot inside? (answer)
Setting abstract type based on typeclass
SYB `cast` function in Scala
In scala 2, can macro or any language feature be used to rewrite the abstract type reification mechanism in all subclasses? How about scala 3?
In Scala 2.13, why is it possible to summon unqualified TypeTag for abstract type?
In Scala 2 inlining can be achieved with Scala 2 macros.
Implicit Json Formatter for value classes in Scala
In https://docs.scala-lang.org/scala3/reference/contextual/relationship-implicits.html#abstract-implicits it's written
An abstract implicit val or def in Scala 2 can be expressed in Scala 3 using a regular abstract definition and an alias given. For instance, Scala 2's
implicit def symDecorator: SymDecorator
can be expressed in Scala 3 as
def symDecorator: SymDecorator
given SymDecorator = symDecorator
You can ask how to override implicit in Scala 3 not changing the definition-site semantics. Probably, just resolving the implicit manually rather than using summon
// Scala 3
trait FooBar[X]:
def value: String
object FooBar:
given intIsFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "a"
trait Intf:
type A
def aIsFoobar: FooBar[A]
given FooBar[A] = aIsFoobar
object IntImpl extends Intf:
override type A = Int
override val aIsFoobar: FooBar[A] = FooBar.intIsFooBar
{
given anotherIntFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "b"
println(IntImpl.aIsFoobar.value) // a
}
More general but less conventional solution would be with Scala 3 macros + compiler internals
// Scala 3.2.1
import scala.quoted.{Quotes, Type, Expr, quotes}
import dotty.tools.dotc.typer.{Implicits => dottyImplicits}
import dotty.tools.dotc.core.Types.{Type => DottyType}
transparent inline def summonSecondBest[A]: A = ${summonSecondBestImpl[A]}
def summonSecondBestImpl[A: Type](using Quotes): Expr[A] =
import quotes.reflect.*
given c: dotty.tools.dotc.core.Contexts.Context =
quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
val typer = c.typer
val search = new typer.ImplicitSearch(
TypeRepr.of[A].asInstanceOf[DottyType],
dotty.tools.dotc.ast.tpd.EmptyTree,
Position.ofMacroExpansion.asInstanceOf[dotty.tools.dotc.util.SourcePosition].span
)
val wildProtoMethod = classOf[typer.ImplicitSearch].getDeclaredField("wildProto")
wildProtoMethod.setAccessible(true)
val wildProto = wildProtoMethod.get(search).asInstanceOf[DottyType]
def eligible(contextual: Boolean): List[dottyImplicits.Candidate] =
if contextual then
if c.gadt.isNarrowing then
dotty.tools.dotc.core.Contexts.withoutMode(dotty.tools.dotc.core.Mode.ImplicitsEnabled) {
c.implicits.uncachedEligible(wildProto)
}
else c.implicits.eligible(wildProto)
else search.implicitScope(wildProto).eligible
def implicits(contextual: Boolean): List[dottyImplicits.SearchResult] =
eligible(contextual).map(search.tryImplicit(_, contextual))
val contextualImplicits = implicits(true)
val nonContextualImplicits = implicits(false)
val contextualSymbols = contextualImplicits.map(_.tree.symbol)
val filteredNonContextual = nonContextualImplicits.filterNot(sr => contextualSymbols.contains(sr.tree.symbol))
val successes = (contextualImplicits ++ filteredNonContextual).collect {
case success: dottyImplicits.SearchSuccess => success.tree.asInstanceOf[ImplicitSearchSuccess].tree
}
successes.tail.head.asExprOf[A]
// Scala 3
trait FooBar[X]:
def value: String
object FooBar:
given intIsFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "a"
trait Intf:
type A
def aIsFoobar: FooBar[A]
given FooBar[A] = aIsFoobar
object IntImpl extends Intf:
override type A = Int
override val aIsFoobar: FooBar[A] = summonSecondBest[FooBar[A]]
{
given anotherIntFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "b"
println(IntImpl.aIsFoobar.value) // a
}
Finding the second matching implicit
Or you can try to make A
a type parameter rather than type member
trait FooBar[X]
object FooBar:
given FooBar[Int] with {}
trait Intf[A: FooBar]
object IntImpl extends Intf[Int]
https://docs.scala-lang.org/scala3/reference/changed-features/implicit-resolution.html
Nesting is now taken into account for selecting an implicit. Consider for instance the following scenario:
def f(implicit i: C) =
def g(implicit j: C) =
implicitly[C]
This will now resolve the implicitly call to j
, because j
is nested
more deeply than i
. Previously, this would have resulted in an
ambiguity error. The previous possibility of an implicit search
failure due to shadowing (where an implicit is hidden by a nested
definition) no longer applies.
@AndreyTyukin's solution:
trait FooBar[X]:
def value: String
object FooBar:
given intIsFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "a"
trait Intf:
type A
def aIsFoobar: FooBar[A]
object implicits:
given FooBar[A] = aIsFoobar
object IntImpl extends Intf:
override type A = Int
override def aIsFoobar: FooBar[A] = summon[FooBar[Int]]
{
given anotherIntFooBar: FooBar[Int] = new FooBar[Int]:
override val value: String = "b"
println(IntImpl.aIsFoobar.value) // a
}
{
import IntImpl.implicits.given
println(summon[FooBar[Int]].value) // a
}
summonInline
looks like it would be changing the semantics too much: I'd have to make the implicit available at the call-cite somehow, but I actually want to avoid this (one of the reasons that I was trying to package everything up inside of anIntf
-module is that I don't have multiple collidinggiven
s flying around at the call-side; Switching tosummonInline
would move in the exactly opposite direction, making the wholeIntf
-setup kind of futile to begin with). β Virgil