Explaining Solidity Reference Types

·

4 min read

Solidity, like many programming languages, has different data types. These data types are grouped into two categories which are the reference types and value types. This grouping is based on the location of their data. If the variable stores its own data, then it belongs to the value type but if it holds the location of the data or points to the location of the data, it belongs to the reference type.

I discussed value types in a previous article. Here, the focus will be on the reference types.

As mentioned above, reference types do not store data directly in their variable, rather they hold the location of that variable. This means that when a change is made to a new variable, such change will reflect on the variable that it points to.

Reference types in Solidity consist of Arrays, Structs, and Mappings.

Array

This is a group of variables that are of the same type and have different locations in the array, known as the index. An index of each variable points to its precise location in the array, and by using that index location you can have access to the variable.

In Solidity, the size of arrays can be fixed or not, which is why there is a fixed size array as well as a dynamic array. The fixed size array has a predefined size during declaration while the dynamic size array does not.

An array that will contain 7 elements of type uint will be declared like this: uint[7] arrayOfSeven And will look like this:

uint[7] arrayOfSeven = [1, 2, 3, 4, 5, 6, 7]

A dynamic array, however, needs no prior specification of size and can be declared in this way: uint[ ] freeArray and used like this:

uint [] freeArray = [2, 4, 6, 9]

With any number of elements, you may want to add to it.

A major difference between both array types is that while the size of the fixed size array is determined before compiling, the size of the dynamic array is determined at run time. Arrays in Solidity have members such as push, pop, and length.

Struct

This is a way of grouping related data together in Solidity. This group can then contain various elements of different types. To define a struct, you’ll need to use the struct keyword. It should look like this:

struct firstStruct {
    int age;
    uint number;
    string name;
}

To use the struct above, we can do something like this:

useStruct = firstStruct(18, 5, “Hafsah”);

In order to access a member of a struct or an item inside a struct, you’ll need to use the member access operator. The member access operator is a period (.) used between the struct name and the member you are trying to access. To access the age member in our struct above, we’ll need to do something like this: firstStruct.age;

When accessing members of structs in Solidity, you may think of objects in JavaScript and how their properties are accessed using the JavaScript dot notation.

Mapping

This is used to store data in a key-value pair. The key of mapping can be of any inbuilt value type, string, and byte. While the value can be any type. Think of mapping as a hashtable or dictionary in some other programming languages. Users can store data in a key-value pair and retrieve the value by using the key.

Mapping is declared using the following syntax: mapping(_keyType => _valueType) variableName; and can be used like this:

(mapping(address => uint) bankAccount );

Mapping has been mentioned as one of the most used reference types in Solidity.

Conclusion

Data types in solidity are a bit different from that of other languages, but once you have an understanding of foundational programming concepts and syntax, you’ll notice that working with them becomes a little easier. Coming from a language like JavaScript, some of these types can be confusing at first, but within a short period of time, you’ll grasp them and get moving.