我正在尝试运行
this math.stackexchange post中提供的Scala代码(请参阅第二个答案),但似乎遇到了以隐式def开头的行中的问题….编译器告诉我错误:预期的定义开始.
有什么想法吗?谢谢!
我应该补充说我正在使用http://www.tutorialspoint.com/compile_scala_online.php来运行我的代码.
解决方法
刚刚在Scala REPL上试过你的例子,它对我有用.
将隐式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) } }
然后做:
import MyImplicits._
这对你有用.