Scala中预期的定义错误开始

我正在尝试运行 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._

这对你有用.

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...