Alec
Alec

Reputation: 32319

Finding the second matching implicit

Consider the following setup:

trait Foo[A]
object Foo extends Priority2

trait Priority0 {
   implicit def foo1: Foo[Int] = new Foo[Int] {}
}
trait Priority1 extends Priority0 {
   implicit def foo2: Foo[Boolean] = new Foo[Boolean] {}
}
trait Priority2 extends Priority1 {
   implicit def foo3: Foo[Double] = new Foo[Double] {}
}

Now, in a REPL (having loaded the above code up), I can do the following:

scala> def implicitlyFoo[A](implicit foo: Foo[A]) = foo
implicitlyFoo: [A](implicit foo: Foo[A])Foo[A]

scala> implicitlyFoo
res1: Foo[Double] = Priority2$$anon$3@79703b86

Is there a way to encode with some typelevel magic that I want to skip over the instances with A =:= Double, but still let type inference figure out what A is?

I do not want to shadow foo3. This is an MVCE: in my real case, foo3 is a def with other implicit arguments (and may play an indirect role in deriving other Foo's).

I've tried =:!= from shapeless but to no avail:

scala> import shapeless._
import shapeless._

scala> def implicitlyFoo2[A](implicit foo: Foo[A], ev: A =:!= Double) = foo
implicitlyFoo2: [A](implicit foo: Foo[A], implicit ev: A =:!= Double)Foo[A]

scala> implicitlyFoo2
<console>:16: error: ambiguous implicit values:
 both method neqAmbig1 in package shapeless of type [A]=> A =:!= A
 and method neqAmbig2 in package shapeless of type [A]=> A =:!= A
 match expected type Double =:!= Double
       implicitlyFoo2
       ^

Upvotes: 3

Views: 196

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51703

Dirty hack is to downcast macro context to its implemenation and use compiler internals.

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

trait Foo[A] {
  def say: String
}

trait Priority0 {
  implicit def foo1: Foo[Int] = new Foo[Int] {
    override def say: String = "int"
  }
}

trait Priority1 extends Priority0 {
  implicit def foo2: Foo[Boolean] = new Foo[Boolean] {
    override def say: String = "bool"
  }
}

trait Priority2 extends Priority1 {
  implicit def foo3: Foo[Double] = new Foo[Double] {
    override def say: String = "double"
  }
}

object Foo extends Priority2


def materializeSecondFoo[A]: Foo[A] = macro impl

def impl(c: whitebox.Context): c.Tree = {
  import c.universe._

  val context = c.asInstanceOf[reflect.macros.runtime.Context]
  val global: context.universe.type = context.universe
  val analyzer: global.analyzer.type = global.analyzer

  var infos = List[analyzer.ImplicitInfo]()

  new analyzer.ImplicitSearch(
    tree = EmptyTree.asInstanceOf[global.Tree],
    pt = typeOf[Foo[_]].asInstanceOf[global.Type],
    isView = false,
    context0 = global.typer.context.makeImplicit(reportAmbiguousErrors = false),
    pos0 = c.enclosingPosition.asInstanceOf[global.Position]
  ) {
    override def searchImplicit(
                                 implicitInfoss: List[List[analyzer.ImplicitInfo]],
                                 isLocalToCallsite: Boolean
                               ): analyzer.SearchResult = {
      val implicitInfos = implicitInfoss.flatten
      if (implicitInfos.nonEmpty) {
        infos = implicitInfos
      }
      super.searchImplicit(implicitInfoss, isLocalToCallsite)
    }
  }.bestImplicit

  val secondBest = infos.tail.head

  global.gen.mkAttributedRef(secondBest.pre, secondBest.sym).asInstanceOf[Tree]
}
materializeSecondFoo.say // bool

Tested in 2.12.8. Motivated by shapeless.Cached.


In 2.13.0 materializeSecondFoo.say should be replaced with

val m = materializeSecondFoo
m.say

The latter is still working in 2.13.10.

Scala 3 implementation:

import scala.quoted.{Quotes, Type, Expr, quotes}
import dotty.tools.dotc.typer.{Implicits => dottyImplicits}

transparent inline def materializeSecondFoo: Foo[_] = ${impl}

def impl(using Quotes): Expr[Foo[_]] = {
  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[Foo[_]].asInstanceOf[dotty.tools.dotc.core.Types.Type],
    dotty.tools.dotc.ast.tpd.EmptyTree,
    Position.ofMacroExpansion.asInstanceOf[dotty.tools.dotc.util.SourcePosition].span
  )

  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(search.wildProto)
        }
      else c.implicits.eligible(search.wildProto)
    else search.implicitScope(search.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[Foo[_]]
}
materializeSecondFoo.say // bool
val foo = materializeSecondFoo
foo: Foo[Boolean] // compiles

Scala 3.2.0.

Upvotes: 0

Related Questions