次のコードは scala シェルでは機能しませんが、IDE では機能します。オブジェクト型を scala-shell のメソッド パラメータとして使用する方法を教えてください。
scala> object A {
| }
defined object A
scala> def f(a:A) :Unit = {
| }
<console>:63: error: not found: type A
def f(a:A) :Unit = {
次のコードは scala シェルでは機能しませんが、IDE では機能します。オブジェクト型を scala-shell のメソッド パラメータとして使用する方法を教えてください。
scala> object A {
| }
defined object A
scala> def f(a:A) :Unit = {
| }
<console>:63: error: not found: type A
def f(a:A) :Unit = {
特性 A を追加するだけです:
trait A
object A extends A
def f(a:A) :Unit = { }
What do you mean by works in IDE? I created a project with one file, HelloWorld1
, where its content is:
object HelloWorld1 extends App {
override def main(args: Array[String]): Unit = {
object A {
}
def f(a:A) :Unit = {}
}
}
When compiling I am getting the following error:
HelloWorld1.scala:8:13
not found: type A
def f(a:A) :Unit = {}
As mentioned in the two answers above, you can either do:
def f(a:A.type) :Unit = {}
Or define trait instead of object:
trait A