Scala 中的预期定义错误开始 [英] Expected start of definition error in Scala
问题描述
我正在尝试运行 this math.stackexchange post(请参阅第二个答案),但似乎在以 implicit def...
.编译器告诉我 error: expected start of definition
.
I'm trying to run the Scala code presented in this math.stackexchange post (please see the second answer), but seem to be running into issues in the line starting with implicit def...
. The compiler is telling me error: expected start of definition
.
有什么想法吗?谢谢!
我应该补充一点,我正在使用 http://www.tutorialspoint.com/compile_scala_online.php 运行我的代码.
I should add that I'm using http://www.tutorialspoint.com/compile_scala_online.php to run my code.
推荐答案
刚刚在 Scala REPL 上尝试了您的示例,它按预期对我有用.
Just tried your example on Scala REPL and it works for me as expected.
将 implicit def
移动到一个对象:
Move the implicit def
to an object:
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._
这应该对你有用.
这篇关于Scala 中的预期定义错误开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!