相当于 Julia 中用于调试的 Rstudio `browser()` 函数

Equivalent of the Rstudio `browser()` function in Julia for debugging

我想知道 RStudio 中是否有与 browser() 语句等效的语句用于 Julia 的调试目的(我目前使用的是 Juno IDE)。

R 函数 browser() 停止执行并在调用时调用 environment browser。因此,原则上,我们可以将 browser() 放在代码中的任何位置以停止在此特定行并查看当时环境中存储的内容,这对于调试目的来说非常有用。

例如,下面的代码将在i>3时停止。因此,这正是我们将在 RStudio 中可用的 environment browser 中看到的内容,我们将在代码中的那一刻观察到 i=4

for (i in 1:5) {
  print(i)  
  if (i>3) {
    browser()
  }
}
[1] 1
[1] 2
[1] 3
[1] 4
Called from: eval(ei, envir)
Browse[1]> 

查看 Debugger.jl. Specifically the Place breakpoints in source code 部分:

It is sometimes more convenient to choose in the source code when to break. This is done for instance in Matlab/Octave with keyboard, and in R with browser(). You can use the @bp macro to do this

你的 R 示例翻译成 Julia:

julia> using Debugger

julia> @run for i in 1:5
           println(i)
           if i > 3
               @bp
           end
       end
1
2
3
4
Hit breakpoint:
In ##thunk#257() at REPL[4]:1
  9  │         Base.println(i)
 10  │   %10 = i > 3
 11  └──       goto #4 if not %10
●12  3 ─       nothing
>13  4 ┄       @_2 = Base.iterate(%1, %8)
 14  │   %14 = @_2 === nothing
 15  │   %15 = ($(QuoteNode(Core.Intrinsics.not_int)))(%14)
 16  └──       goto #6 if not %15
 17  5 ─       goto #2

About to run: (iterate)(1:5, 4)
1|debug>

这是 Julia 的通用解决方案,Juno IDE 还集成了调试:Debugging, Juno manual

Infiltrator.jl@infiltrate 在 Julia 中似乎是等价的:

julia> using Infiltrator

julia> for i in 1:5
         println(i)
         if i > 3
           @infiltrate
         end
       end
1
2
3
4
Infiltrating top-level scope at REPL[1]:4:

infil> i
4

与Debugger.jl的断点相比,这根本不会减慢程序的执行速度,代价是不允许您进一步进入程序。