f# - 是否可以强制 .NET F# 编译器在模块中生成 CIL 字段?

标签 f# cil

当我编译 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/

相关文章:

c# - CLR 如何知道静态字段是否已被初始化?

c# - 将 lambda 作为 IL 流传递给辅助 AppDomain,然后使用 DynamicMethod 将其组装回去

c# - 为什么 callvirt IL 指令会导致虚方法中的递归调用?

linq-to-sql - F# 3.0 LINQ groupBy 示例错误

f# - 使用 Fable-Elmish 上传文件

c# - 高效的 Int32/Uint32 排序映射/稀疏数组

c# - 澄清 IL 生成的引用字符串的代码

F# 将空引用传递给 ref 参数

asp.net-mvc - F# Global.asax -- 不支持语言?

c# - Silverlight 3.0 C# - 使用 Methodbuilder 创建 SET 方法,并使用 ILGenerator 和 Emit 添加 MSIL