Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala case class private apply method( repl bug ?)

in Scala2.10.0 REPL

Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_13).
Type in expressions to have them evaluated. 
Type :help for more information.

scala> case class A private(i:Int)
defined class A

scala> A(1)
res0: A = A(1)

But if compile

$ scala -version
Scala code runner version 2.10.0 -- Copyright 2002-2012, LAMP/EPFL
$ cat Main.scala 
package foo

case class A private (i:Int)

object Main extends App{
  println(A(1))
}

$ scalac Main.scala 
Main.scala:6: error: constructor A in class A cannot be accessed in object Main
  println(A(1))
          ^
one error found

A.apply(1) is compile error.

is this Scala2.10.0 REPL bug?

FYI Scala2.9.2 REPL is following

Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_13).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class A private(i:Int)
defined class A

scala> A(1)
<console>:10: error: constructor A in class A cannot be accessed in object $iw
              A(1)
              ^
like image 738
Kenji Yoshida Avatar asked Feb 12 '13 11:02

Kenji Yoshida


1 Answers

This definitely looks like a REPL bug.

Note that the constructor is correctly marked as private (in other words, new A(1) does not compile, as expected), it is only the factory (A.apply) that is wrongly public.

like image 73
Régis Jean-Gilles Avatar answered Oct 04 '22 15:10

Régis Jean-Gilles