-1

次のコードは 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 = {
4

3 に答える 3

0

特性 A を追加するだけです:

trait A
object A extends A
def f(a:A) :Unit = { }
于 2018-11-21T12:38:51.410 に答える
0

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
于 2020-10-12T08:24:46.183 に答える