Ken*_*ida 7 constructor scala case-class read-eval-print-loop
在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)
Run Code Online (Sandbox Code Playgroud)
但如果编译
$ 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
Run Code Online (Sandbox Code Playgroud)
A.apply(1)
是编译错误.
这个Scala2.10.0 REPL错误?
FYI Scala2.9.2 REPL如下
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)
^
Run Code Online (Sandbox Code Playgroud)
这绝对看起来像一个 REPL 错误。
请注意,构造函数被正确标记为private
(换句话说,new A(1)
没有按预期进行编译),只有工厂 ( A.apply
) 被错误地公开。