在 Scala (Eclipse IDE) 中使用特征和伴随对象时返回值时出现问题

Problems returning a value while using a trait and companion object in Scala (Eclipse IDE)

我已将以下代码放入 Eclipse 中的 object.Scala 文件中,并且只想知道 "x" 的值是多少(应该是 3)。如果我将值放在 List 对象以外的任何地方,代码将无法编译;同时,在 List 对象中,运行对文件不产生任何输出。

package work

object Work extends App {
  // These 3 lines define a simplified List type
  sealed trait List[+A]
  case object Nil extends List[Nothing]
  case class Cons[+A](head: A, tail: List[A]) extends List[A]

  // This companion object contains basic operations for the List type
  object List {
    def sum(ints: List[Int]): Int = ints match {
      case Nil => 0
      case Cons(x,xs) => x + sum(xs)
    }

    def product(ds: List[Double]): Double = ds match {
      case Nil => 1.0
      case Cons(0.0, _) => 0.0
      case Cons(x,xs) => x * product(xs)
    }

    def apply[A](as: A*): List[A] =
      if (as.isEmpty) Nil
      else Cons(as.head, apply(as.tail: _*))

    // The next line is the value that I want to return. 
    val x = List(1,2,3,4,5) match {
      case Cons(x, Cons(2, Cons(4, _))) => x
      case Nil => 42
      case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y 
      case Cons(h, t) => h + sum(t)
      case _ => 101
     }

  }
}

我最初将所有内容都放在 object Work extends App 中以便编译,否则编译器会抱怨我缺少 Main 方法。但是,为了返回 "x",我尝试放弃 Work 对象 ,而是 向我希望看到的表达式添加了一个 Main 方法,如下所示:

def main(args: Array[String]) {
      val x = List(1,2,3,4,5) match {
        case Cons(x, Cons(2, Cons(4, _))) => x
        case Nil => 42
        case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y case Cons(h, t) => h + sum(t)
        case _ => 101
      }
}

但是,我收到以下错误:

List has a main method with parameter type Array[String], but work.List will not be a runnable program. Reason: companion is a trait, which means no static forwarder can be generated.

我不太确定这是什么意思。我对如何正确实现 Main 方法也很困惑。我尝试直接在List对象下添加一个Main方法,封装了List的所有定义和val x,结果比上面的错误还要多。我很困惑,仍在网上搜索答案,但到目前为止似乎没有解决我的特定问题。

如有任何帮助,我们将不胜感激。

下面是我的原文 post,包含更多信息,包括我正在做的练习的文本。为了完成练习,我需要了解表达式的计算方式。正是出于这个原因,我通常在 Scala Worksheet 中工作,因为我可以在几秒钟内看到我的结果,但是 这个特定的练习涉及使用特征和伴随对象,并且它们的添加对我来说使事情变得非常复杂,同时我了解它们在代码中的使用方式,但我不知道如何正确实现它们。


原版Post


我将以下代码放入 Eclipse IDE 的 Scala Worksheet 中,然后花了大约两个小时尝试不同的方法来制作它

  1. 编译无误,
  2. 在右侧显示评估结果。

我仍在为 (2) 苦苦挣扎。

package fpinscala.datastructures
// The following three lines define a simplified List type and behavior
sealed trait List[+A] 
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]

// The companion object List defines basic List operations
object List { 
  def sum(ints: List[Int]): Int = ints match {
    case Nil => 0 
    case Cons(x,xs) => x + sum(xs) 
  }

  def product(ds: List[Double]): Double = ds match {
    case Nil => 1.0
    case Cons(0.0, _) => 0.0
    case Cons(x,xs) => x * product(xs)
  }

  def apply[A](as: A*): List[A] = 
    if (as.isEmpty) Nil
    else Cons(as.head, apply(as.tail: _*))
}

当我 运行 上面的代码时,它输出一个标准错误,指出缺少主要参数。虽然我不太清楚 def main(args: Array[String) {}object some_object extends App {} 的实际工作方式,但我知道它们允许代码实际编译和 运行。 与此同时,Scala Worksheet 过去从不要求我有 Main 方法,尽管过去我也没有在同一个文件中使用 classes 或 traits。 我知道我一定错过了什么。

我试过了

其中一些已编译,另一些尚未编译,但到目前为止 none 已导致工作表在右侧实际产生评估结果。

这是我一直在努力完成的需要上述代码的练习,要是我能真正看到我的代码是如何计算的就好了:

Let’s try implementing a few different functions for modifying lists in different ways. You can place this, and other functions that we write, inside the List companion object.

3.2 Implement the function tail for removing the first element of a List. Note that the function takes constant time. What are different choices you could make in your implementation if the List is Nil? We’ll return to this question in the next chapter.

来自 Functional Programming in Scala.

非常感谢您的宝贵时间。

我不确定你为什么提到将 val x 放在 List 对象之外会阻止你编译。

以下编译并产生预期输出 = 3

object Work extends App {
  // These 3 lines define a simplified List type
  sealed trait List[+A]
  case object Nil extends List[Nothing]
  case class Cons[+A](head: A, tail: List[A]) extends List[A]

  // This companion object contains basic operations for the List type
  object List {
    def sum(ints: List[Int]): Int = ints match {
      case Nil => 0
      case Cons(x,xs) => x + sum(xs)
    }

    def product(ds: List[Double]): Double = ds match {
      case Nil => 1.0
      case Cons(0.0, _) => 0.0
      case Cons(x,xs) => x * product(xs)
    }

    def apply[A](as: A*): List[A] =
      if (as.isEmpty) Nil
      else Cons(as.head, apply(as.tail: _*))
  }

  // The next line is the value that I want to return.
  val x = List(1,2,3,4,5) match {
    case Cons(x, Cons(2, Cons(4, _))) => x
    case Nil => 42
    case Cons(x, Cons(y, Cons(3, Cons(4, _)))) => x + y
    case Cons(h, t) => h + List.sum(t)
    case _ => 101
  }

  println(x)
}