Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Companion Object implicit val ordering issues #7830

Closed
scabug opened this issue Sep 10, 2013 · 4 comments
Closed

Companion Object implicit val ordering issues #7830

scabug opened this issue Sep 10, 2013 · 4 comments

Comments

@scabug
Copy link

scabug commented Sep 10, 2013

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.

@scabug
Copy link
Author

scabug commented Sep 10, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7830?orig=1
Reporter: William Thurston (jhspaybar)
Affected Versions: 2.10.1
See #7264, #801

@scabug
Copy link
Author

scabug commented Sep 11, 2013

@retronym said:
Sounds similar or the same as #7264.

@scabug
Copy link
Author

scabug commented Sep 11, 2013

@retronym said:
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.

@scabug scabug closed this as completed Sep 11, 2013
@scabug
Copy link
Author

scabug commented Sep 11, 2013

William Thurston (jhspaybar) said:
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant