.net – 如何在F#中定义类型成员常量?

栏目: ASP.NET · 发布时间: 7年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/2399917/how-to-define-type-member-constant-in-f

在C#中,可以像这样定义一个类型成员常量:

class Foo { public const int Bar = 600; }

IL看起来像这样.

.field public static literal int32 Bar = int32(600)

如何在Visual F#/ FSharp中执行相同操作?

我试过这个没有效果:

[<Sealed>]
 type Foo() =

    [<Literal>]
    let Bar = 600

我用F#编译器进行了几次实验,这里有一些我的观察.如果要创建IL文字,则需要将标记为Literal的值放在模块中.例如:

module Constants = 
  [<Literal>]
  let Num = 1

作为旁注,我通过F#规范进行了快速搜索,似乎文字对于模式匹配来说非常有用,因为您可以将它们用作模式(只要以大写字母开头):

open Constants
match 1 with
| Num -> "1"
| _ -> "other"

现在,问题在于,为什么Literal不会像您在类型声明中放置时所期望的那样.我认为原因是,在F#类型声明中的let声明不能被公开,并且只能在类/类型内部可见.我相信当你使用它们时,C#和F#的内联字面值都是类型声明.然而,由于文字不能公开,所以没有任何理由可以生成字面的IL字段,因为没有人可以访问它.

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/2399917/how-to-define-type-member-constant-in-f


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning iPhone and iPad Web Apps

Beginning iPhone and iPad Web Apps

Chris Apers、Daniel Paterson / Apress / 2010-12-15 / USD 39.99

It seems that everyone and her sister has developed an iPhone App—everyone except you, the hard-working web professional. And now with the introduction of the iPad, you may even feel farther behind. B......一起来看看 《Beginning iPhone and iPad Web Apps》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换