gpt4 book ai didi

f# - 为什么 F# 交互式控制台不认为 "assert (2=3)"是错误的?

转载 作者:行者123 更新时间:2023-12-03 15:42:18 25 4
gpt4 key购买 nike

我将此行发送到 Visual Studio 中的 F# 交互式控制台

assert (2=3)
出乎我的意料,控制台没有报错,但是
val it : unit = ()
同样,如果我跑
printf ("hello!")

assert (2=3)

printf ("hello2")
在 REPL 上,我收到了“hellohello2”而没有任何错误消息。
我怎样才能让 F# 交互告诉我 2=3 是错误的?

最佳答案

封面下,assert关键字转换为对 Debug.Assert 的方法调用.NET 库中的方法 ( see the method documentaiton )。这有一个条件编译属性 [Conditional("DEBUG")] ,这意味着只有在符号 DEBUG 时才包含调用被定义为。
默认情况下,在 F# Interactive 中不是这种情况。你可以通过添加 --define:DEBUG 来做到这一点。到 fsi.exe 的命令行参数.这将位于编辑器选项中的某个位置,具体取决于您使用的内容。例如,在 Visual Studio 中,您需要这样的东西:
enter image description here
编辑 : 这样的东西如果不想修改命令行参数怎么办?这实际上取决于您想要什么样的行为。 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)

关于f# - 为什么 F# 交互式控制台不认为 "assert (2=3)"是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62991945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com