当我编译 F# 模块时
module internal Test =
let testnum = 5
let testfun = fun (s, p) -> System.Console.WriteLine(s, p)
生成的 IL 代码中的前一个成员成为一个属性,其 getter 仅返回常量,而后者则被编译为真正的函数。本质上,生成 IL 的 C# 类似于
internal static class Test {
internal static int testnum { get { return 5; } }
internal static void testfun(string s, object p) {
System.Console.WriteLine(s, p);
}
}
是否有任何已知的方法可以强制将它们编译到真正的 CIL 字段中?正如
internal static class Test {
internal static readonly int testnum = 5;
internal static readonly FSharpFunc<Tuple<string, object>, Unit> testfun =
new SomeInternallyGeneratedClosure();
}
}
最佳答案
为了在 F# 中声明公共(public)字段,您需要使用 explicit field在类声明上。
类声明中的 Let 绑定(bind)不允许附加访问修饰符,并且是私有(private)的,因此它们不能用于创建公共(public)字段。
关于f# - 是否可以强制 .NET F# 编译器在模块中生成 CIL 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387554/