5

When I try to use '\0' as a character in F# it doesn't work. Here is what I see:

enter image description here

I have read elsewhere that Char.MinValue will accomplish the same thing though.

Is there any reason why '\0' is not supported?

Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
Nathaniel Elkins
  • 719
  • 1
  • 9
  • 16
  • I think the error is misleading. It's not a valid escape sequence. You just need three digits, but that has already been answered. – Abel May 29 '20 at 18:35

2 Answers2

6

The F# specification describes the grammar for char literals:

3.5 Strings and Characters

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

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

regexp simple-char-char= | (any char except '\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

regexp char-char= | simple-char-char | escape-char | trigraph | unicodegraph-short

\0 matches none of these - if you want the null character you can use

let c = '\000'

or Char.MinValue

Lee
  • 142,018
  • 20
  • 234
  • 287
5

It is supported. Character literals in F# are either a unicode character or the 16-bit unicode number. It's just that the backslash means it's a short or long escape sequence.

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

The ASCII version of this is '\nnn'. So

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

And so \0 becomes \000.

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

Please note that the unicode specifier is in hex, while the short sequence is in decimal.

DEC(65) = HEX(41)

Asti
  • 12,447
  • 29
  • 38