Cadence Types: Exploring Data Structures

Types of data strucutres

1. Boolean

Boolean has two values true and false with the type Bool

Constants are declared using the let keyword.

2. Numeric Literals

Numbers can be written in various bases. Numbers are assumed to be decimal by default. Non-decimal literals have a specific prefix.

Underscores are allowed for all numeral systems.

2.1 Decimals

prefix none

Decimal numbers may contain underscores (_) to logically separate components.

1234567890 // is 1234567890

// A decimal number with leading zeros. Not an octal number!

00123 // is 123

2.2 Binary

prefix 0b only 0,1 allowed

0b101010 // is 42

2.3 Octal

prefix 0o only 0-7 allowed

0o12345670 // is 2739128

2.4 Hexadecimal

prefix 0x only 0-9 and a-f allowed

0x1234567890ABCabc // is 1311768467294898876

2.5 Integers

Integers are numbers without a fractional part. They are either signed (positive, zero, or negative) or unsigned (positive or zero).

let smallNumber: UInt8 = 10

We will talk more about integers in the coming chapter

2.6 Fixed-Point Numbers

Fixed-point numbers are useful for representing fractional values. They have a fixed number of digits after decimal point.

They are essentially integers which are scaled by a factor. For example, the value 1.23 can be represented as 1230 with a scaling factor of 1/1000. The scaling factor is the same for all values of the same type and stays the same during calculations.

2.7 Floating-Point Numbers

There is no support for floating point numbers.

3. Addressess

The type Address represents an address. Addresses are unsigned integers with a size of 64 bits (8 bytes). Hexadecimal integer literals can be used to create address values.

Address can also be created using a byte array or string.

let someAddress: Address = 0x436164656E636521

let addressFromString: Address? = Address.fromString("0x436164656E636521")

4. Strings

Strings are collections of characters. Strings have the type String, and characters have the type Character. Strings can be used to work with text in a Unicode-compliant way. Strings are immutable.

String and character literals are enclosed in double quotation marks

The type Character represents a single, human-readable character. Characters are extended grapheme clusters, which consist of one or more Unicode scalars.

let someString = "Hello, world!"

5. Arrays

Arrays are mutable, ordered collections of values. Arrays may contain a value multiple times. Array literals start with an opening square bracket [ and end with a closing square bracket ].

We will talk more about arrays in the coming chapter

[1, 2, 3]

6. Dictionary

Dictionaries are mutable, unordered collections of key-value associations. Dictionaries may contain a key only once and may contain a value multiple times. Dictionary literals start with an opening brace { and end with a closing brace } . Keys are separated from values by a colon, and key-value associations are separated by commas.

We will talk more about dictinary in the coming chapter

let booleans = {

1: true,

0: false

}

8.Never

Never is the bottom type, i.e., it is a subtype of all types. There is no value that has type Never. Never can be used as the return type for functions that never return normally. For example, it is the return type of the function panic.

Minimum and maximum values

The minimum and maximum values for all integer and fixed-point number types are available through the fields min and max.

let max = UInt8.max

// max is 255, the maximum value of the type UInt8

let max = UInt8.min

// min is 0, the maximum value of the type UInt8