Scala中预期的定义错误开始

sta*_*711 5 scala

我正在尝试运行此math.stackexchange帖子中提供的Scala代码(请参阅第二个答案),但似乎是在开始时遇到问题implicit def....编译器告诉我error: expected start of definition.

有什么想法吗?谢谢!

我应该补充一点,我正在使用http://www.tutorialspoint.com/compile_scala_online.php来运行我的代码.

Ven*_*ama 7

刚试过你在Scala REPL上的例子,它对我有用.

移动implicit def到一个对象:

object MyImplicits {
  /** Pimp `Set[X]` with a few convenient operators */
  implicit def logicalSetOps[X](set: Set[X]) = new {
    def and(other: Set[X]) = set.intersect(other)
    def or(other: Set[X]) = set.union(other)
    def minus(other: Set[X]) = set.filterNot(other.contains)
  }
}
Run Code Online (Sandbox Code Playgroud)

然后做:

import MyImplicits._
Run Code Online (Sandbox Code Playgroud)

这对你有用.