【问题标题】:does order of function definitions in IntelliJ IDEA's scala worksheet matter?IntelliJ IDEA scala 工作表中函数定义的顺序重要吗?
【发布时间】:2021-10-13 05:54:32
【问题描述】:

我正在尝试在 IntelliJ IDEA 中评估 Scala 工作表(sbt 1.5.5,jvm 采用@1.11.0-11,Scala 3.0.1)。 worksheet.sc,如果定义如下

def abs(x:Double) = if (x < 0) -x else x

def sqrtIter(guess: Double, x: Double): Double =
  if (isGoodEnough(guess, x)) guess
  else sqrtIter(improve(guess, x), x)

def isGoodEnough(guess: Double, x: Double) =
  abs(guess * guess - x ) / x < 0.001

def improve(guess: Double, x: Double) =(guess + x / guess) / 2

在评估时确实会引发一些错误:Not found: isGoodEnoughNot found: improve
但是,如果我将这两个函数的定义移到上方 sqrtIter,它就会正确运行。

函数定义的顺序重要吗?

【问题讨论】:

  • 是的,工作表可能类似于在 scala REPL 上写东西。 REPL 无法知道将来会输入什么。
  • 我想你已经回答了你自己的问题。

标签: scala intellij-idea jvm sbt


【解决方案1】:

是的,在使用运行类型为 REPL 的 Scala 工作表时,顺序确实很重要。

来自下面的 cmets:
显示工作表设置(点击左上角的扳手图标)。将 Run type 从 REPL 更改为 Plain。您的代码将按照发布的方式编译(无需重新排序)。

【讨论】:

  • 显示工作表设置(点击左上角的扳手图标)。将 运行类型REPL 更改为 Plain。您的代码将按照发布的方式编译(无需重新排序)。
猜你喜欢
  • 2023-02-20
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
相关资源
最近更新 更多