f# - F# 标识符、模块、类型和成员名称中允许使用哪些字符?

标签 f# clr member naming let-binding

这个问题是关于标识符中的字符,而不是 keywords as identifiers .

我找到了this question on C# names ,但在 F# 上找不到相同的。通常这几乎不相关,但在我的测试命名中,我经常使用点 .并且很惊讶它在模块名称中不受支持,但在 let-binding 中受支持:

// fails:
module ``Run Test.Me functions`` = 
    [<Test>]
    let ``X.Add should add``() = test <@ X.Add 2 2 = 4 @>

// Succeeds
module ``Run Test-Me functions`` 
    [<Test>]
    let ``X.Add should add``() = test <@ X.Add 2 2 = 4 @>

在命名测试之外,我看不出它有多大用处,但这让我想知道:类型和模块名称支持哪些字符,成员名称和 let 绑定(bind)支持哪些字符?

一些测试:
module ``Weird.name`` = ()  // fails
module ``Weird-name`` = ()  // succeeds
module ``Weird()name`` = () // succeeds (?)
module ``Weird*name`` = ()  // fails
module ``Weird+name`` = ()  // fails
module ``Weird%name`` = ()  // succeeds (?)
module ``Weird/name`` = ()  // fails
module ``Weird\\name`` = () // fails

所有这些名称都在 let-binding 或成员名称中成功,但不是作为类型名称或模块名称。至少这是一致的。但是我在允许和不允许的内容中找不到任何线路或逻辑

也许限制是由 CLR/MSIL 而不是 F# 本身施加的?

最佳答案

看看F# Language Specification 4.0 - 在部分 3.4 Identifiers and Keywords .

Note that when an identifier is used for the name of a types, union type case, module, or namespace, the following characters are not allowed even inside double-backtick marks:
., +, $, &, [, ], /, \\, *, \", `



除了这个列表,@ (at-sign) 可以使用任何名称,但会引发警告:

warning FS1104: Identifiers containing '@' are reserved for use in F# code generation



在我能找到的附近:

字符列表可以在 F# 编译器中找到,名称为 IllegalCharactersInTypeAndNamespaceNames。 .

由于这用于生成 IL,因此会导致 ECMA-335 - Common Language Infrastructure (CLI) Partitions I to VI内容如下:

II.5.3 Identifiers - Identifiers are used to name entities. Simple identifiers are equivalent to an ID. However, the ILAsm syntax allows the use of any identifier that can be formed using the Unicode character set (see Partition I). To achieve this, an identifier shall be placed within single quotation marks.

ID is a contiguous string of characters which starts with either an
alphabetic character (A–Z, a–z)
or one of _, $, @, ` (grave accent), or ?,
and is followed by any number of
alphanumeric characters (A–Z, a–z, 0–9)
or the characters _, $, @, ` (grave accent), and ?

关于f# - F# 标识符、模块、类型和成员名称中允许使用哪些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274652/

相关文章:

c# - 在这种情况下,垃圾收集器会做什么?

c# - GCHandle.FromIntPointer 没有按预期工作

c++ - C++中的二叉搜索树复制构造函数

c# - 即使在所有者类别中,也使成员只能通过属性访问吗?

f# - 多个数据成员属性

f# - FSharp 在基类中调用泛型方法

f# - 是否有任何工具可以帮助将 F# 移植到 OCaml?

c# - 如果你改变它们的身份,HashSets 不会保持元素的唯一性

f# - 通过类型提供者创建通用类型

c++ - 是否可以创建一个函数来比较从对象 vector 中动态选择的变量?