gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:35:49 25 4
gpt4 key购买 nike

这个问题是关于标识符中的字符,而不是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com