Search code examples
scalaprivate-members

Impossible to make inner object's method private for outside


In the following example, I can't get to hide update from public exposure:

trait Order {
  sealed trait EntryOption {
    private[Order] def update(e: EntryOption): Unit
  }

  private case object EmptyEntry extends EntryOption {
    def update(e: EntryOption) = ()
  }

  trait Entry extends EntryOption

  def test(a: Entry, b: EntryOption): Unit = a.update(b)
}

It fails to compile with "error: object creation impossible, since method $line12$$read$Order$^date in trait EntryOption of type (e: Order.this.EntryOption)Unit is not defined" – whatever that is supposed to be (compiler bug?). I tried the following without success:

  • Also make update in EmptyEntry private[Order]
  • Make it protected – this breaks method test

The goal is to have EntryOption's update inaccessible from outside Order.

EDIT

If I tentatively change trait Order to object Order it compiles, indicating a potential compiler bug?


Solution

  • A stupid work-around:

    trait Order {
      sealed trait EntryOption {
        private[Order] def update(e: EntryOption): Unit
      }
    
      private sealed trait ScalaChokes extends EntryOption {
        private[Order] final def update(e: EntryOption) = ()
      }
    
      private case object EmptyEntry extends ScalaChokes
    
      trait Entry extends EntryOption
    }
    

    Should I file a bug?