저는 Python 배경에서 왔으며 코드의 어느 시점에서든 추가 할 수 있습니다.
import pdb; pdb.set_trace()
그리고 런타임에 나는 그 지점에서 인터랙티브 인터프리터로 떨어질 것입니다. 스칼라에 상응하는 것이 있습니까, 아니면 런타임에 불가능합니까?
답변
예, Scala 2.8에서 가능합니다. 이 작업을 수행하려면 클래스 경로에 scala-compiler.jar를 포함해야합니다. 를 사용하여 스칼라 프로그램을 호출하면 scala
자동으로 수행됩니다 (또는 제가 만든 테스트에 나타납니다).
그런 다음 다음과 같이 사용할 수 있습니다.
import scala.tools.nsc.Interpreter._
object TestDebugger {
def main(args: Array[String]) {
0 to 10 foreach { i =>
breakIf(i == 5, DebugParam("i", i))
println(i)
}
}
}
여러 DebugParam
인수를 전달할 수 있습니다 . REPL이 나오면 오른쪽에있는 값은 왼쪽에 입력 한 이름의 값에 바인딩됩니다. 예를 들어 다음과 같이 해당 줄을 변경하면 :
breakIf(i == 5, DebugParam("j", i))
그러면 다음과 같이 실행됩니다.
C:\Users\Daniel\Documents\Scala\Programas>scala TestDebugger
0
1
2
3
4
j: Int
scala> j
res0: Int = 5
을 입력하여 실행을 계속합니다 :quit
.
또한 무조건 호출하여 REPL로 떨어질 수 break
수신 List
의 DebugParam
대신 가변 인자의. 다음은 전체 예제, 코드 및 실행입니다.
import scala.tools.nsc.Interpreter._
object TestDebugger {
def main(args: Array[String]) {
0 to 10 foreach { i =>
breakIf(i == 5, DebugParam("j", i))
println(i)
if (i == 7) break(Nil)
}
}
}
그리고:
C:\Users\Daniel\Documents\Scala\Programas>scalac TestDebugger.scala
C:\Users\Daniel\Documents\Scala\Programas>scala TestDebugger
0
1
2
3
4
j: Int
scala> j
res0: Int = 5
scala> :quit
5
6
7
scala> j
<console>:5: error: not found: value j
j
^
scala> :quit
8
9
10
C:\Users\Daniel\Documents\Scala\Programas>
답변
Daniel의 답변에 추가하기 위해 Scala 2.9부터 break
및 breakIf
메서드는 scala.tools.nsc.interpreter.ILoop
. 또한 DebugParam
현재 NamedParam
입니다.
답변
IntelliJ IDEA :
- 디버그 모드에서 실행하거나 원격 디버거 연결
- 중단 점을 설정하고 도달 할 때까지 실행
Evaluate Expression
( Alt+ F8, 메뉴 : Run-> Evaluate Expression) 창을 열어 임의의 Scala 코드를 실행합니다.- 실행할 코드 조각 또는 표현식을 입력하고 평가를 클릭하십시오.
- Alt+를 입력 V하거나 평가를 클릭하여 코드 조각을 실행합니다.
식:
스칼라 2.10으로 모두 break
와 breakIf
에서 제거되었습니다 ILoop
.
통역사에 침입하려면 ILoop
직접 협력해야 합니다.
먼저 scala compiler
라이브러리를 추가하십시오 . Eclipse Scala의 경우 프로젝트 => Build Path
=> Add Libraries...
=>를 마우스 오른쪽 버튼으로 클릭하십시오 Scala Compiler
.
그런 다음 인터프리터를 시작하려는 위치에서 다음을 사용할 수 있습니다.
import scala.tools.nsc.interpreter.ILoop
import scala.tools.nsc.interpreter.SimpleReader
import scala.tools.nsc.Settings
val repl = new ILoop
repl.settings = new Settings
repl.settings.Yreplsync.value = true
repl.in = SimpleReader()
repl.createInterpreter()
// bind any local variables that you want to have access to
repl.intp.bind("row", "Int", row)
repl.intp.bind("col", "Int", col)
// start the interpreter and then close it after you :quit
repl.loop()
repl.closeInterpreter()
Eclipse Scala에서 인터프리터는 Console
보기 에서 사용할 수 있습니다 .