何时评估模块中的f#“ do”语句?
我对何时应该评估的事情有点困惑。
我有文件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 do
s at a top-level scope in module A are executed? Am I not able to rely on do
s in module B being run before I can use the types and functions from module A (which calls B)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它发生在其他任何地方对模块的首次引用时发生。
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.我也很惊讶
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.