Fsharp: Literal Types

By Xah Lee. Date: .

String

// type: String
// meaning: Unicode string
// suffix: none
printfn "%A" "ab🤡"
// "abc"
printfn "%O" ("ab🤡".GetType())
// System.String

Char

// type: Char
// meaning: Unicode character
// suffix: none
printfn "%A" 'a'
// 'a'
printfn "%A" '\u0061'
// 'a'
printfn "%O" ('a'.GetType())
// System.Char

Byte

Unsigned Byte

// type: Byte
// meaning: unsigned 8-bit natural number. 0 to 255, inclusive
// suffix: uy
printfn "%A" 255uy
// 255uy
printfn "%O" (255uy.GetType())
// System.Byte

// printfn "%A" 256uy
// error FS1144: This number is outside the allowable range for 8-bit unsigned integers

// turn a ASCII character into byte
printfn "%A" 'a'B
// 97uy
printfn "%O" ('a'B.GetType())
// System.Byte

Signed Byte

// type: SByte
// meaning: signed 8-bit integer
// suffix: y
printfn "%A" -127y
// -127y
printfn "%O" (-127y.GetType())
// System.SByte

// warning, must be between -127 and 127 inclusive
// printfn "%A" 128y
// error FS1142: This number is outside the allowable range for 8-bit signed integers

Byte Array

// type: Byte[]
// meaning: ASCII string; byte array
// suffix: B
printfn "%A" "abc"B
// [|97uy; 98uy; 99uy|]
printfn "%O" ("abc"B.GetType())
// System.Byte[]

// unicode not allowed
// printfn "%A" "🤡"B
// This byte array literal contains characters that do not encode as a single byte

Integer

Signed Integer

// type: Int16
// meaning: signed 16-bit integer. -32768 to 32768, inclusive
// suffix: s
printfn "%A" -32768s
// -32768s
printfn "%O" (-32768s.GetType())
// System.Int16

// type: int Int32
// meaning: signed 32-bit integer. -2147483648 to 2147483648, inclusive
// suffix: l or none
printfn "%A" -2147483648l
// -2147483648
printfn "%O" (-2147483648l.GetType())
// System.Int32

// type: Int64
// meaning: signed 64-bit integer. max is 9.223372036854776e+18 -1
// suffix: L
printfn "%A" 1234567890123456789L
// 1234567890123456789L
printfn "%O" (1234567890123456789L.GetType())
// System.Int64

Big integer

// type: BigInteger
// meaning: integer not limited to 64-bit representation
// suffix: I
printfn "%A" 9999999999999999999999999999I
printfn "%O" (9999999999999999999999999999I.GetType())
// System.Numerics.BigInteger

Int Pointer

// type: IntPtr
// meaning: native pointer to a signed natural number
// suffix: n
printfn "%A" 123n
// 123n
printfn "%O" (123n.GetType())
// System.IntPtr
// type: UIntPtr
// meaning: native pointer as an unsigned natural number
// suffix: un
printfn "%A" 0x0000ace8un
// 44264un
printfn "%O" (0x0000ace8un.GetType())
// System.UIntPtr

Unsigned Integer

// type: UInt16
// meaning: unsigned 16-bit natural number. 0 to 65535 inclusive
// suffix: us
printfn "%A" 65535us
// 65535us
printfn "%O" (65535us.GetType())
// System.UInt16

// type: uint uint32
// meaning: unsigned 32-bit natural number. 0 to 4_294_967_295 inclusive
// suffix: u or ul
printfn "%A" 4_294_967_295u
// 4294967295u
printfn "%A" 4_294_967_295ul
// 4294967295u
printfn "%O" (4_294_967_295u.GetType())
// System.UInt32
printfn "%O" (4_294_967_295ul.GetType())
// System.UInt32

// type: uint64
// meaning: unsigned 64-bit natural number. max 18446744073709551615 inclusive
// suffix: UL
printfn "%A" 3UL
printfn "%O" (3UL.GetType())

Float

// type: Single; float32
// meaning: 32-bit floating point number
// suffix: F or f
printfn "%A" 8.25f
// printfn "%A" lf
// printfn "%A" 0x00000000lf
printfn "%O" (8.25f.GetType())
// System.Single

printfn "%A" infinityf
printfn "%O" (infinityf.GetType())
// System.Single
printfn "%A" -infinityf

// type: float double
// meaning: 64-bit floating point number
// suffix: none
printfn "%A" 8.25
printfn "%A" 2.3E+32
printfn "%A" 2.3e+32
printfn "%A" 2.3e-32
printfn "%A" infinity
printfn "%A" -infinity
// printfn "%A" LF
// printfn "%A" 0x0000000000000000LF

// type: decimal
// meaning: fractional number represented as a fixed point or rational number
// suffix: M or m
printfn "%A" 0.7833M
printfn "%A" 0.7833m

Type Description Suffix Examples
sbyte signed 8-bit integer y 86y 0b00000101y
byte unsigned 8-bit natural number uy 86uy 0b00000101uy
int16 signed 16-bit integer s 86s
uint16 unsigned 16-bit natural number us 86us
int int32 signed 32-bit integer l or none 86 86l
uint uint32 unsigned 32-bit natural number u or ul 86u 86ul
nativeint native pointer to a signed natural number n 123n
unativeint native pointer as an unsigned natural number un 0x00002D3Fun
int64 signed 64-bit integer L 86L
uint64 unsigned 64-bit natural number UL 86UL
single float32 32-bit floating point number F or f 4.14F or 4.14f or 2.3e+32f or 2.3e-32f or infinityf or -infinityf lf 0x00000000lf
float double 64-bit floating point number none 4.14 or 2.3E+32 or 2.3e+32 or 2.3e-32 or infinity or -infinity LF 0x0000000000000000LF
bigint integer not limited to 64-bit representation I 9999999999999999999999999999I
decimal fractional number represented as a fixed point or rational number M or m 0.7833M or 0.7833m
Char Unicode character none 'a' or '\u0061'
String Unicode string none "abc"
byte ASCII character B 'a'B
byte[] ASCII string B "text"B
String or byte[] verbatim string @ prefix @"abc" @"abc"B

Fsharp, type