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

Implicit resolution of parameters is different depending on whether return types of implicits are inferred.

    Details

      Description

      This session from my REPL is probably more explanatory than I can be:

      scala> case class Class1()
      defined class Class1
      
      scala> case class Class2(implicit class1: Class1) {
           |   def printClass1() = println(class1)
           | }
      defined class Class2
      
      scala> object Test {
           |   val class2 = new Class2
           |   implicit val class1 = new Class1()
           | }
      <console>:12: error: could not find implicit value for parameter class1: Class1
               val class2 = new Class2
                            ^
      
      scala> object Test {
           |   val class2 = new Class2
           |   implicit val class1: Class1 = new Class1()
           | }
      defined module Test
      
      scala> Test.class2.printClass1()
      null
      
      scala> object Test {
           |   implicit val class1 = new Class1()
           |   val class2 = new Class2
           | }
      defined module Test
      
      scala> Test.class2.printClass1()
      Class1()
      

      This is an example of using implicit parameters as a basic inversion of control in the first definition of the module Test the compiler is correctly failing as there is no definition at that point. However, in the second definition of Test, the compiler appears to allow this through, but passes the reference to class1 before it is initialized (confirmed using -Xprint:typer). The final definition is the fully valid case.

      It would appear that specifying the type of class1 somehow causes that code to be valid.

        Issue Links

          Activity

          Hide
          Paul Phillips added a comment -

          I wanted to say "as specified", but I can't find it in the spec. If it's not in there it's way overdue to be in there. Anyway, this is "as intended." See comments in SI-801.

          Show
          Paul Phillips added a comment - I wanted to say "as specified", but I can't find it in the spec. If it's not in there it's way overdue to be in there. Anyway, this is "as intended." See comments in SI-801 .
          Hide
          Sean Parsons added a comment -

          The comments in that bug are for implicit conversions aren't they? In either case I'd say this is pretty confusing in its current state.

          Show
          Sean Parsons added a comment - The comments in that bug are for implicit conversions aren't they? In either case I'd say this is pretty confusing in its current state.
          Hide
          Paul Phillips added a comment -

          It is about implicit eligibility.

          Show
          Paul Phillips added a comment - It is about implicit eligibility.
          Hide
          Aleksandar Prokopec added a comment -

          Test case added in pending/neg.

          Show
          Aleksandar Prokopec added a comment - Test case added in pending/neg.
          Hide
          Grzegorz Kossakowski added a comment -

          Unassigning and rescheduling to M7 as previous deadline was missed.

          Show
          Grzegorz Kossakowski added a comment - Unassigning and rescheduling to M7 as previous deadline was missed.

            People

            • Assignee:
              Adriaan Moors
              Reporter:
              Sean Parsons
            • Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:

                Development