Chapter 3. Primitive Types

WHAT'S IN THIS CHAPTER?

  • Understanding primitive types

  • Declaring primitive type instances

  • Applying operators

Like all languages that run on top of the CLR, the F# language provides a core set of primitive types that offer basic integer and floating-point arithmetic capabilities, character string support, Boolean types, and so on. In general, these map to the corresponding CLS types (System.Int16, System.Int32, and so on), as described next, but a few types are new to F# and come from the F# libraries. These types are fully accessible to other languages, such as C# and Visual Basic but obviously have no native language support there and need to be used as any other .NET type is (that is, via fully qualified type names).

BOOLEAN

Probably the simplest primitive type in F# is the bool type, which corresponds to the CLR's underlying System.Boolean type, and has two possible values, true and false.

Booleans support the usual range of logical operations, including && (logical AND) and || (logical OR), and otherwise behave just as Boolean values do in any other .NET language.

NUMERIC TYPES

F# supports a wide range of numeric types, 8 bits in size to 64, in both signed and unsigned versions, as shown here:

TYPE

DESCRIPTION

.NET NAME

LITERALS

Byte

8-bit unsigned integer

System.Byte

3uy, 0xFuy

Sbyte

8-bit signed integer

System.SByte

3y, 0xFy

int16

16-bit signed integer

System.Int16

3s, 0xFs

uint16

16-bi unsigned integer

System.UInt16

3us, 0xFus

int, int32

32-bit signed integer

System.Int32

3, 0xF ...

Get Professional F# 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.