简体   繁体   English

如何在F#interactive中使用断点?

[英]How do I use breakpoints in F# interactive?

I've started researching some ideas in algorithms using VS2010 and F# interactive. 我已经开始使用VS2010和F#interactive研究算法中的一些想法。

So, I've created a DebugScript.fsx , I write some code there and eventually send it to F#Int to test it. 所以,我创建了一个DebugScript.fsx ,我在那里编写了一些代码并最终将它发送到F#Int来测试它。

At some some moment I need to catch a bug. 在某些时候我需要抓住一个bug。 But I can't place a breakpoint even in a simple for loop: 但即使在一个简单的for循环中我也无法设置断点:

for i in stringarray do
    printfn "%s" i

When I press F9 to set a breakpoint, the VS show a red circle with a warning sign. 当我按F9设置断点时,VS会显示一个带有警告标志的红色圆圈。 The hint for it is "The breakpoint will not currently be hit". 它的提示是“断点当前不会被击中”。

Surely, I did open Debug menu -> Attach to process ... -> Fsi.exe previously 当然,我确实打开了Debug菜单 - > Attach to process ... - > Fsi.exe

I tried placing Debugger.Break() inside the loop, but that's the only line where debugger stops, giving me no option to proceed with debugging lines inside the loop. 我尝试在循环中放置Debugger.Break() ,但这是调试器停止的唯一行,这使我无法继续在循环内部调试行。 I also don't have any local variables available :( 我也没有任何局部变量:(

Maybe I'm missing something obvious? 也许我错过了一些明显的东西?

in VS2015 is now much more easy: in Tools->Option->F# tools -> F# interactive-> Debugging -> Enable script debugging = True 在VS2015中现在要简单得多:在工具 - >选项 - > F#工具 - > F#interactive-> Debugging - >启用脚本调试= True

then reset your interactive session and when you want to debug press CTRL+ALT+D and send command as usual 然后重置您的交互式会话,当您想要调试按CTRL + ALT + D并像往常一样发送命令

(or right click on the source file and choose "debug in interactive") (或右键单击源文件并选择“在交互式调试”)

You cannot debug the code that is part of your scripts, but you can debug external code referenced in your scripts by attaching the debugger to the Fsi.exe process as you describe. 您无法调试作为脚本一部分的代码,但您可以通过将调试器附加到Fsi.exe进程来调试脚本中引用的外部代码。 So if you really want to debug from the interactive window without wanting to write a console program, you can create an fs file with the function to debug and: 因此,如果您真的想从交互式窗口进行调试而不想编写控制台程序,则可以使用要调试的函数创建一个fs文件,并且:

  1. Attach the debugger 附加调试器

  2. Either

    • reference the fs file with #load (*) 用#load(*)引用fs文件
    • compile the code as a library and reference the resulting DLL with #r 将代码编译为库并使用#r引用生成的DLL
  3. Set a breakpoint 设置断点

  4. Call the function from a script 从脚本调用该函数

(*) worked on my VS Ultimate installation, but I haven't been able to reproduce on another machine with VS Shell install (*)在我的VS Ultimate安装上工作,但我无法在VS Shell安装的另一台机器上重现

As far as I know, there's no way to do this. 据我所知,没有办法做到这一点。 Instead, you'll want to use a .fs file and start debugging it in Visual Studio instead. 相反,您将需要使用.fs文件并在Visual Studio中开始调试它。

In addition to kvb's answer: One could say that F# Interactive is already a type of debugger. 除了kvb的回答:可以说F#Interactive已经是一种调试器了。 You can feed code line by line (or region by region) and see intermediate results. 您可以逐行(或逐个区域)提供代码并查看中间结果。 You can also inspect the value of bindings just by evaluating them in the F# Interactive console. 您还可以通过在F#Interactive控制台中评估绑定的值来检查绑定的值。

the interactive is for feeding it small pieces of code and evaluating intermediate results, think of it as micro-unit testing. 交互式是为了提供小块代码并评估中间结果,将其视为微单元测试。 i've had some success with using nunit with f#, it's no different than with c#, you decorate your testfixture and test cases in the same way and it will load up. 我使用带有f#的nunit取得了一些成功,它与c#没有什么不同,你以同样的方式装饰你的testfixture和测试用例,它会加载。 i did have some issues with mixed-mode assemblies where nunit would barf, but overall, it's a better way imho to test a piece of code. 我确实有一些混合模式程序集的问题,其中nunit会barf,但总的来说,这是一个更好的方式来测试一段代码。 also, i did not see a way to send a namespace to interactive, only modules. 另外,我没有看到将命名空间发送到仅交互式模块的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM