【问题标题】:Is there a reason '\0' is not supported in F#?F# 不支持 '\0' 是否有原因?
【发布时间】:2020-09-17 07:57:31
【问题描述】:

当我尝试在 F# 中使用 '\0' 作为字符时,它不起作用。这是我看到的:

我已经读到elsewhereChar.MinValue 将完成同样的事情。

'\0' 不支持有什么原因吗?

【问题讨论】:

  • 我认为该错误具有误导性。这不是一个有效的转义序列。你只需要三个数字,但已经回答了。

标签: .net f#


【解决方案1】:

F# specification 描述了 char 字面量的语法:

3.5 字符串和字符

regexp escape-char= '\' ["\'ntbrafv]

regexp non-escape-chars= '\' [^"\'ntbrafv]

正则表达式 simple-char-char= | (除 '\n' '\t' '\r' '\b' '\a' 以外的任何字符 '\f' '\v' ' \")

regexp unicodegraph-short = '\''u' hexdigit hexdigit hexdigit hexdigit

regexp unicodegraph-long= '\''U' hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit

regexp trigraph = '\' digit-char digit-char digit-char

正则表达式 char-char= |简单字符字符 |转义字符 |三字组 | unicodegraph-short

\0 不匹配这些 - 如果你想要空字符,你可以使用

let c = '\000'

Char.MinValue

【讨论】:

    【解决方案2】:

    支持。 F# 中的字符文字是 unicode 字符或 16 位 unicode 数字。只是反斜杠表示它是一个短或长的转义序列。

    let A = '\u0041' // Capital letter A
    

    它的 ASCII 版本是 '\nnn'。所以

    let A = '\065' // Capital letter A
    

    所以\0 变成了\000

    assert('\000' = Char.MinValue) // true
    

    请注意,unicode 说明符是十六进制的,而短序列是十进制的。

    DEC(65) = HEX(41)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多