scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Private constructors of case class defined in method body should not be accessible outside of the class/object definitions #8821

Closed scabug closed 5 years ago

scabug commented 10 years ago

A private constructor of case class is only accessible in the class/companion object definition. Trying to access from anywhere else results a compilation error.

// CodeInBody.scala
object CodeInBody {
  case class A private (i: Int)
  val a1 = A(1)    // compilation error
}

Except if the case class is defined in a method body.

// CodeInMethod.scala
object CodeInMethod {
  def f {
    case class A private (i: Int)
    val a1 = A(1)   // no compilation error
  }
}

I would expect the same behavior in both cases.

scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8821?orig=1 Reporter: @skyluc Affected Versions: 2.11.2

scabug commented 10 years ago

@skyluc said: Related Scala IDE worksheet tickets: https://github.com/scala-ide/scala-worksheet/issues/194 https://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1002237

scabug commented 9 years ago

@jsuereth said: I think I ran into an instance of this recently with combined vs. separate compilation: https://gist.github.com/jsuereth/bf94458a0d03035b58c3

scabug commented 8 years ago

@som-snytt said: The related fix makes the behavior uniform.

The equivocation about the apply and the ctor is common parlance:

http://stackoverflow.com/questions/38097490/scala-case-class-private-constructor-isnt-private