我有以下 F# 程序:
open MyModule
printfn "%d" test
MyModule 为:
module MyModule
printfn "foo"
let test =
printfn "bar"
42
这会产生以下输出:
foo
bar
42
当我将 MyModule 更改为:
module MyModule
printfn "foo"
let test =
// printfn "bar" <-- note the comment!
42
...结果是:
42
为什么不再打印“foo”?
最佳答案
我认为规范第 12.5.1 节 Execution of Static Initializers ,有你的答案。引用相关位:
the static initializer for the file is executed on first access of a value that has observable initialization
和
All definitions have observable initialization except for the following definitions in modules:
以下列表包括:
Non-mutable, non-thread-local values that are bound to a simple constant expression
注释掉第一行test
后,就变成了常量表达式。因此,它不再触发静态初始化。
编辑
规范没有提供此行为的基本原理,但它与 C# 类似。例如,在此代码中,静态初始化永远不会发生:
class Program {
static void Main(string[] args) {
Console.WriteLine(T.Integer);
Console.WriteLine(T.Null);
Console.WriteLine(T.Enum);
Console.Read();
}
}
static class T {
static T() {
Console.WriteLine("You won't see this.");
}
public const int Integer = 1;
public const string Null = null;
public const ConsoleKey Enum = ConsoleKey.Escape;
}
关于f# - F# 模块初始化的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17575323/