不鼓励使用副作用的nullary方法:建议为IntelliJ定义为....

时间:2015-10-27 05:58:42

标签: scala intellij-idea

我在IntelliJ工作表中执行了此代码块(在JDK 1.8.0_66上使用Scala插件1.9.4的Community Edition EAP 15#143.379.11)

class Plant
class Fruit extends Plant
class Apple extends Fruit

class Box[T <: Fruit](var item: T) {
  def get: T = item
  def replace(item: T): Unit = this.item = item
}

val appleBox = new Box(new Apple)
println(appleBox.get) // error

并且IntelliJ在工作表编译期间报告此错误并停止,

Error:(22, -59) side-effecting nullary methods are discouraged: suggest defining as `def get$$instance$$res0()` instead
println(appleBox.get);//
    ^

如何禁用此错误或将其更改为警告并让我继续?我正在使用IntelliJ。感谢

1 个答案:

答案 0 :(得分:0)

为了避免错误,只需删除println,Scala Worksheet将为您打印对象。

    val appleBox = new Box(new Apple)
    appleBox.get

    appleBox: Box[Apple] = Box@4109bbc4
    res0: Apple = Apple@6e2e3a8b

PS:我没有收到您在最新的EAP Scala插件(1.9.272)中报告的错误