6

I am using scala 2.10.0-snapshot dated (20120522) and have the following Scala files:

this one defines the typeclass and a basic typeclass instance:

package com.netgents.typeclass.hole

case class Rabbit

trait Hole[A] {
  def findHole(x: A): String
}

object Hole {
  def apply[A: Hole] = implicitly[Hole[A]]
  implicit val rabbitHoleInHole = new Hole[Rabbit] {
    def findHole(x: Rabbit) = "Rabbit found the hole in Hole companion object"
  }
}

this is the package object:

package com.netgents.typeclass

package object hole {

  def findHole[A: Hole](x: A) = Hole[A].findHole(x)

  implicit val rabbitHoleInHolePackage = new Hole[Rabbit] {
    def findHole(x: Rabbit) = "Rabbit found the hole in Hole package object"
  }
}

and here is the test:

package com.netgents.typeclass.hole

object Test extends App {

  implicit val rabbitHoleInOuterTest = new Hole[Rabbit] {
    def findHole(x: Rabbit) = "Rabbit found the hole in outer Test object"
  }

  {
    implicit val rabbitHoleInInnerTest = new Hole[Rabbit] {
      def findHole(x: Rabbit) = "Rabbit found the hole in inner Test object"
    }

    println(findHole(Rabbit()))
  }
}

As you can see, Hole is a simple typeclass that defines a method which a Rabbit is trying to find. I am trying to figure out the implicit resolution rules on it.

  • with all four typeclass instances uncommented, scalac complains about ambiguities on rabbitHoleInHolePackage and rabbitHoleInHole. (Why?)

  • if I comment out rabbitHoleInHole, scalac compiles and I get back "Rabbit found the hole in Hole package object". (Shouldn't implicits in the local scope take precedence?)

  • if I then comment out rabbitHoleInHolePackage, scalac complains about ambiguities on rabbitHoleInOuterTest and rabbitHoleInInnerTest. (Why? In the article by eed3si9n, url listed below, he found implicits btw inner and outer scope can take different precedence.)

  • if I then comment out rabbitHoleInInnerTest, scalac compiles and I get back "Rabbit found the hole in outer Test object".

As you can see, the above behaviors do not follow the rules I've read on implicit resolution at all. I've only described a fraction of combinations you can do on commenting/uncommenting out instances and most of them are very strange indeed - and I haven't gotten into imports and subclasses yet.

I've read and watched presentation by suereth, stackoverflow answer by sobral, and a very elaborate revisit by eed3si9n, but I am still completely baffled.

Community
  • 1
  • 1
Walter Chang
  • 11,547
  • 2
  • 47
  • 36
  • the behaviors are different than in 2.9.2 * with all four typeclass instances uncommented, scalac compiles and i get back "Rabbit found the hole in inner Test object" * if i commented out rabbitHoleInHole, scalac compiles and i get back "Rabbit found the hole in inner Test object". * if i then commented out rabbitHoleInHolePackage, scalac compiles and i get back "Rabbit found the hole in inner Test object". * if i then commentted out rabbitHoleInInnerTest, scalac compiles and i get back "Rabbit found the hole in outer Test object". – Walter Chang May 26 '12 at 14:02
  • There was a bug in the inference rules with Scala up to 2.9, which was actually uncovered by someone here on Stack Overflow who tried to understand how the spec dictated the behavior he was seeing. – Daniel C. Sobral May 27 '12 at 07:03
  • does that mean i should treat the implicit resolution behaviors i observed in 2.10 as the correct behaviors going forward and regard the behaviors in 2.9 as a temporal anomaly? – Walter Chang May 27 '12 at 08:29

1 Answers1

4

Let's start with the implicits in the package object and the type class companion disabled:

package rabbit {
  trait TC

  object Test  extends App {
    implicit object testInstance1 extends TC { override def toString = "test1" }

    {
      implicit object testInstance2 extends TC { override def toString = "test2" }

      println(implicitly[TC])
    }
  }
}

Scalac looks for any in scope implicits, finds testInstance1 and testInstance2. The fact that one is in a tighter scope is only relevant if they have the same name -- the normal rules of shadowing apply. We've chosen distinct names, and there neither implicit is more specific than the other, so an ambiguity is correctly reported.

Let's try another example, this time we'll play off an implicit in the local scope against one in the package object.

package rabbit {
  object `package` {
    implicit object packageInstance extends TC { override def toString = "package" }
  }

  trait TC

  object Test  extends App {
    {
      implicit object testInstance2 extends TC { override def toString = "test2" }

      println(implicitly[TC])
    }
  }
}

What happens here? The first phase of the implicit search, as before, considers all implicits in scope at the call site. In this case, we have testInstance2 and packageInstance. These are ambiguous, but before reporting that error, the second phase kicks in, and searches the implicit scope of TC.

But what is in the implicit scope here? TC doesn't even have a companion object? We need to review the precise definition here, in 7.2 of the Scala Reference.

The implicit scope of a type T consists of all companion modules (§5.4) of classes that are associated with the implicit parameter’s type. Here, we say a class C is associated with a type T, if it is a base class (§5.1.2) of some part of T.

The parts of a type T are:

  • if T is a compound type T1 with ... with Tn, the union of the parts of T1, ..., Tn, as well as T itself,
  • if T is a parameterized type S[T1, ..., Tn], the union of the parts of S and T1,...,Tn,
  • if T is a singleton type p.type, the parts of the type of p,
  • if T is a type projection S#U, the parts of S as well as T itself,
  • in all other cases, just T itself.

We're searching for rabbit.TC. From a type system perspective, this is a shorthand for: rabbit.type#TC, where rabbit.type is a type representing the package, as though it were a regular object. Invoking rule 4, gives us the parts TC and p.type.

So, what does that all mean? Simply, implicit members in the package object are part of the implicit scope, too!

In the example above, this gives us an unambiguous choice in the second phase of the implicit search.

The other examples can be explained in the same way.

In summary:

  • Implicit search proceeds in two phases. The usual rules of importing and shadowing determine a list of candidates.
  • implicit members in an enclosing package object may also be in scope, assuming you are using nested packages.
  • If there are more than one candidate, the rules of static overloading are used to see if there is a winner. An addiotnal a tiebreaker, the compiler prefers one implicit over another defined in a superclass of the first.
  • If the first phase fails, the implicit scope is consulted in the much same way. (A difference is that implicit members from different companions may have the same name without shadowing each other.)
  • Implicits in package objects from enclosing packages are also part of this implicit scope.

UPDATE

In Scala 2.9.2, the behaviour is different and wrong.

package rabbit {
  trait TC

  object Test extends App {
    implicit object testInstance1 extends TC { override def toString = "test1" }

    {
      implicit object testInstance2 extends TC { override def toString = "test2" }

      // wrongly considered non-ambiguous in 2.9.2. The sub-class rule
      // incorrectly considers:
      //
      // isProperSubClassOrObject(value <local Test>, object Test)
      //   isProperSubClassOrObject(value <local Test>, {object Test}.linkedClassOfClass)
      //   isProperSubClassOrObject(value <local Test>, <none>)
      //     (value <local Test>) isSubClass <none>
      //        <notype> baseTypeIndex <none> >= 0
      //        0 >= 0
      //        true
      //     true
      //   true
      // true
      //
      // 2.10.x correctly reports the ambiguity, since the fix for
      //
      // https://issues.scala-lang.org/browse/SI-5354?focusedCommentId=57914#comment-57914
      // https://github.com/scala/scala/commit/6975b4888d
      //
      println(implicitly[TC])
    }
  }
}
Community
  • 1
  • 1
retronym
  • 54,768
  • 12
  • 155
  • 168
  • BTW, `scalac -Yinfer-debug` helps out to the candidates and chosen implicits. – retronym May 26 '12 at 19:37
  • yes, i can dup the same behaviors in 2.10.x and your explaination of the implicit resolution is quite clear. however, the behaviors i observed using 2.9.2 were quite different. in the first case where you disabled both implicits in package object and typeclass companion object, the code compiled without any ambiguity warning and returned "test2" when ran. in the second case with the implicit in the package object enabled, the code compiled as stated but "test2" was printed instead of "package". has the treatment of implicit resolution changed in 2.10.x? – Walter Chang May 27 '12 at 06:49
  • Looks like a subtle bug in [`isProperSubClassOrObject`](https://github.com/scala/scala/blob/v2.9.2/src/compiler/scala/tools/nsc/typechecker/Infer.scala#L984) -- it doesn't account for the fact that `sym2` might be a local block, which has `` as it's companion. I'm haven't figured out what changed in 2.10.x to avoid this. – retronym May 27 '12 at 09:24
  • because package object are in scope for both phases, it creates a situation which i think is rather counterintuitive: implicits defined locally can NOT override the ones that are defined in package objects. is my understanding correct? – Walter Chang May 27 '12 at 14:13
  • 1
    In this example implicits in the package object are candidates in both phases *only* because the type classes are defined in the same package as the call site that performs the implicit search. You can always disable an implicit in an outer scope (e.g. an package object), by shadowing it, that is, defining the a local implicit with the same name. In general, I don't recommend putting implicits in package objects. – retronym May 27 '12 at 16:24
  • i did find, however, even when the implicit and the call site are in two different packages, if the implicit is defined in one of the package object, it is still candidate in both phases. i do think your suggestion of not putting implicits in package objects a very worthy rule to follow though. – Walter Chang May 27 '12 at 17:11
  • Can you provide an example of that? – retronym May 28 '12 at 17:13
  • my bad. after putting the Test object in another package, i imported the package containing the typeclass via wildcard, thus imported its package object as well. after fixing the problem by using explicit imports, the package object from the typeclass package is not considered in phase 1 by the compiler anymore, just as you explained earlier. – Walter Chang May 29 '12 at 03:40