Understanding Data Types In Cairo

Understanding Data Types In Cairo

Cairo is a statically typed language. This means the compiler must know how to deal with the values provided to it at compile time.

In some cases, the compiler can infer the data type from its usage but this is not always advisable.

There are two subsets of data types in Cairo namely Scalar and Compounds.

SCALAR TYPES

Scalar types consist of Integers, Booleans, and felts. These three are the meat and potatoes regarding scalar data types in Cairo.

Felts: Another name for felts is "Field Elements". This data type is also the default type for all Cairo variables. A felt element is an integer in the range of 0 <= X < P . This means the felt element can be any number that is

  1. Greater than or equal to 0 but cannot be less than 0

  2. Less than P. Cannot be greater than P where P is a very large prime number

For more in-depth information about felts check the bible of Cairo: the Cairo book here.

Integers: Simply put integers are numbers without fractional components. i.e. Whole numbers without fractions

eg. 2, 45, 90.

The below table shows built-in data types in Cairo that can be used to declare the type of any integer.

TypeUnsignedLength
8 bitu8255
16 bitu1665535
32 bitu324294967295
64 bitu6418446744073709551615
128 bitu128340282366920938463463374607431768211455
256 bitu256115792089237316195423570985008687907853269984665640564039457584007913129639935

Basic arithmetic operations can be carried out on all integers. Another thing to note is that once a data type is declared every value assigned to it must fit within the range of the data type.

An example of basic mathematical operations that can be performed on integers is addition lets see this in action.

Run this code scarb cairo-run and you should get something like the below if everything checks out.

Let's say we declared a variable number_u8 with data type u8. In the code block below we assigned a value of 255 to the variable which is well within the range of the specified data type. what happens?

When we run this code everything checks out and we do not have the compiler screaming at us.

What happens if just by mistake we assign a value that does not fit within the length of the data type?

The compiler fumes at us and sends us an error expressing its displeasure.

Booleans: This type contains just two possible values only and cannot have both possible values at the same. The two values are true and false. A boolean is either true or false at a time and cannot be true and false at the same time.

COMPOUND TYPES

Compound types consist of two types tuples and unit type. For this blog, we will talk about tuples only.

Tuples: This is a clever way of grouping various types in one compound type. Take for example basic biodata. This could contain your name which is a string and your cell number which is an integer.

Tuples help us to have a compound type with other types that are closely related.

They have fixed lengths meaning once declared they cannot grow or shrink in size.

To use the values in a tuple it has to be destructured. If you come from javascript you would understand this concept.

Conclusion

A solid grasp of data types will eliminate allocating little or too much memory to a variable. This will in turn make your Starknet smart contracts run efficiently and keep transaction costs low.

You can leave any questions or comments you have in the comments section.