我将此行发送到 Visual Studio 中的 F# 交互式控制台
assert (2=3)
Run Code Online (Sandbox Code Playgroud)
出乎我的意料,控制台没有报错,但是
val it : unit = ()
Run Code Online (Sandbox Code Playgroud)
同样,如果我跑
printf ("hello!")
assert (2=3)
printf ("hello2")
Run Code Online (Sandbox Code Playgroud)
在 REPL 上,我收到了“hellohello2”而没有任何错误消息。
我怎样才能让 F# 交互告诉我 2=3 是错误的?
Tom*_*cek 10
在幕后,assert关键字转换为Debug.Assert对 .NET 库中方法的方法调用(请参阅方法文档)。这具有条件编译属性[Conditional("DEBUG")],这意味着仅当符号出现时才包含调用DEBUG定义。
默认情况下,在 F# Interactive 中不是这种情况。您可以通过添加这样做--define:DEBUG,为的命令行参数fsi.exe。这将位于编辑器选项中的某个位置,具体取决于您使用的内容。例如,在 Visual Studio 中,您需要这样的东西:
编辑:如果您不想修改命令行参数,该怎么做?这实际上取决于您想要什么样的行为。的默认行为assert是它显示一个消息框,您可以在其中终止程序或忽略错误。你可以使用:
open System.Windows.Forms
let ensure msg b =
let res =
MessageBox.Show("Assertion failed: " + msg +
"\n\nDo you want to terminate the exection? Press 'Yes' " +
"to stop or 'No' to ignore the error.", "Assertion failed",
MessageBoxButtons.YesNo)
if res = DialogResult.Yes then exit 42
ensure "Mathematics is broken" (2=3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |