何时评估模块中的f#“ do”语句?

发布于 2025-02-11 21:55:43 字数 428 浏览 1 评论 0原文

我对何时应该评估的事情有点困惑。

我有文件A.fs:

type MyType = { ... }

do System.Console.Write("A.fs")

在文件B.FS中:

let x : A.MyType = { ... }
do System.Console.Write("B.fs")

我希望在B.FS之前的某个时候看到A.FS。但是“ A.fs”实际上根本没有打印出来。

我不知道它是否相关,但是A.FS和B.FS在不同的库中。

当执行模块A中的顶级范围 s时,执行 s的规则是什么?在我可以使用模块A(调用B)的类型和功能之前,我无法依靠模块B中的 s s吗?

I'm a little confused with when exactly things are supposed to evaluate.

I have file A.fs:

type MyType = { ... }

do System.Console.Write("A.fs")

In file B.fs:

let x : A.MyType = { ... }
do System.Console.Write("B.fs")

I would expect to see A.fs printed sometime before B.fs. But "A.fs" is not actually printed at all.

I don't know if it's related, but A.fs and B.fs are in different libraries.

What are the rules for when dos at a top-level scope in module A are executed? Am I not able to rely on dos in module B being run before I can use the types and functions from module A (which calls B)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2025-02-18 21:55:43

它发生在其他任何地方对模块的首次引用时发生。

F#模块被编译为.NET静态类,并且其所有do行都放在静态构造函数中。当类“加载”类时,在.NET中执行.NET中的静态构造函数,这是第一次尝试访问类中的任何内容时发生的。

通常,您不应依靠do行以进行任意副作用。理想情况下,您根本不应该拥有它们,但是如果必须,将它们的功能限制为初始化模块功能所需的内容。

It happens at the time of first reference to the module from anywhere else.

F# modules are compiled as .NET static classes, and all their do lines are put in the static constructor. Static constructors in .NET are executed when the class is "loaded", which happens the first time somebody tries to access anything in the class.

As a general rule, you shouldn't rely on the do lines for arbitrary side-effects. Ideally you shouldn't have them at all, but if you must, limit their functionality to initializing stuff that's directly required for the module to function.

浪荡不羁 2025-02-18 21:55:43

我也很惊讶do语句有时无法评估。请参阅我在 f#repo 的问题。它似乎是根据规格。

参见 fsharp语言4.1 静态初始化器。

I was also surprised that do statements sometimes don't get evaluated. See my question on the F# repo. It seems to be according to spec.

see FSharp language specification 4.1, 12.5.1 execution of static initializers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文