Scala Programming Language
  1. Scala Programming Language
  2. SI-7830

Companion Object implicit val ordering issues

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: Scala 2.10.1
    • Fix Version/s: None
    • Component/s: Compiler Backend
    • Labels:
    • Environment:

      Scala 2.10.1 compiler

      Description

      http://stackoverflow.com/questions/18724500/implicits-compiler-bug

      Also pasted below

      First, a short description. It seems that if I put an implicit for some other type in a companion object and import that implicit into scope of the class it isn't found during implicit resolution until it is referenced explicitly once(or defined above the class). Example below.

       object Main extends App {
           
          class A(val a: String) { override def toString = s"A: $a"}
           
          object A {
            implicit val default = new A("A") //found by default
          }
           
          object B {
            def func(fn: => Int)(implicit a: A) = println(a)
          }
           
          class Broken {
            def doSomething = {
            import Broken._ // or Broken.actual
            B.func { 123 } // Uses A.default, not Broken.actual for implicit
            }
          }
           
          object Broken {
            implicit val actual = new A("Broken")
          }
           
          class Fixed {
            def doSomething = {
              import Fixed._
              println(actual) //reference actual
              B.func { 123 } // uses Fixed.actual
            }
          }
           
          object Fixed {
            implicit val actual = new A("Fixed")
          }
           
          object WTF {
            implicit val actual = new A("WTF")
          }
           
          class WTF {
            def doSomething = {
              import WTF._
              B.func { 123 } // With object definition first this works without referencing actual
            }
          }
           
          (new Broken).doSomething
          (new Fixed).doSomething
          (new WTF).doSomething
        }
      

      output:

      A: A
      A: Fixed
      A: Fixed
      A: WTF
      http://ideone.com/1Vtepf

      If object A doesn't have a default implicit it does error out saying no implicits are found as in the related Jira.

      The problem I have though is that it compiles and finds what is obviously NOT the expected implicit. I understand this has been addressed in many situations of "implicit not found" but I've yet to see one addressed as "wrong implicit found" which I think is much worse and why I've opened this bug report.

        Issue Links

          Activity

          Hide
          Jason Zaugg added a comment -

          Sounds similar or the same as SI-7264.

          Show
          Jason Zaugg added a comment - Sounds similar or the same as SI-7264 .
          Hide
          Jason Zaugg added a comment -

          Can you please: post a complete example (no `...`), and show a transcript of one or more calls to scalac that includes the compiler errors? If you can test this out on 2.11.0-M4 or later it would also help to see if this is a duplicate.

          Show
          Jason Zaugg added a comment - Can you please: post a complete example (no `...`), and show a transcript of one or more calls to scalac that includes the compiler errors? If you can test this out on 2.11.0-M4 or later it would also help to see if this is a duplicate.
          Hide
          William Thurston added a comment -

          The output of a successful compilation is linked at the bottom of the jira with full code in all elided areas. I will include it here as well. There is also NO ERROR in this place. It simply doesn't exhibit what the behavior should be. It finds the object A default rather than Broken.actual

          Show
          William Thurston added a comment - The output of a successful compilation is linked at the bottom of the jira with full code in all elided areas. I will include it here as well. There is also NO ERROR in this place. It simply doesn't exhibit what the behavior should be. It finds the object A default rather than Broken.actual

            People

            • Assignee:
              Unassigned
              Reporter:
              William Thurston
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development