ETHEREUM SMART CONTRACTS DEVELOPMENT

Solidity Fundamentals: Types

Reference Types

Ferdi Kurt
Coinmonks
Published in
2 min readDec 16, 2020

--

In this part, we will examine reference typesarrays, struct and mapping which store the location of the data, a reference, and don’t share the data directly. It is obvious that we should handle these kind of types with more caution since we are dealing with locations , otherwise we can easily lose performance.

Arrays are group of elements of the same data type in which each element has a particular location called index. Size of arrays can be fixed or dynamic.

In Solidity, A[5] is always an array containing five elements of type A, even if A is itself an array. This is not the case in other languages such as C.

Arrays have the following members — length, push(), push(x), pop(). Let’s look at each of them with some examples;

Structs in Solidity allows to declare new types in the form of structs. This type is a group of different types, which can contain both value types and reference types. With one necessary restriction — it is not possible for a struct to contain a member of its own type as the size of the struct has to be finite. This does not mean struct can’t contain struct, for instance struct A can contain struct B but struct A can’t contain its own type struct A.

Mappings stores data in a key-value pair where a key can be any value types. We can think this reference type like hash tables or dictionary in any other programming languages, where data can be retrieved by key.

Structs work really well with mappings and arrays, but at first glance it is a bit tricky to get used to it.

All reference type has an additional annotation, the data location, about where it is stored. There are three possible options: memory , storage,and calldata. But we will look at data location and assignment behavior in the next article.

Feel free to ask any question.

Stay safe, do good work, and keep in touch!

Ferdi Kurt

--

--