Commonly used types in solidity

Commonly used types in solidity

Let's discuss some commonly used types in solidity such as addresses, strings, structs, ...

·

4 min read

In your journey to becoming a solidity developer, you must always remember that solidity is a statically typed language . This implies that every variable should have a type on declaration. Let's say you want to store "wagmi" in a variable called "name". In this case you should specify that the type of name is string because that variable name will store a string.

Let's now talk about types in solidity

String

A string is basically a word or a collection of words. These should be in quotation marks to be considered as string type.

string name = "wagmi"

Integer

In simple words, an integer refers to a number. In solidity, we have two types of integers:

  • Unsigned integers(uint): these are just numbers that don't accept the minus(-) sign before them or in other words they are always positive.
    uint256 randomNumber = 91;
    
  • Signed integers(int): integers of this category can either be positive or negative . This means they can also hold a negative sign
    int256 positiveNumber = 44;
    int256 negativeNumber = -21;
    
    In solidity, integers can hold up to 256bits.

Address

An address holds a 20 byte value which is a certain location on the blockchain. This can be the location of a certain address or a contract. You can think of it as the address of a certain house on earth. The string "24, Zion street, Poland" is the address of a certain house or building , the same way "0x28fD274a7D8Bb794076309aEA8D0aa2ae6DF17F1" is the address of a certain account on the Ethereum blockchain and this is unique. In solidity we have 2 types of addresses:

  • Address : this is just a normal address as stated above.
  • Payable address : this is an address where you can send cryptocurrency.
    Note: You can convert address to payable address in solidity. The code below converts address x into a payable address and store that in a new variable y.
    address x = "0x28fD274a7D8Bb794076309aEA8D0aa2ae6DF17F1";
    address payable y = payable(x)
    

Struct

A struct is basically a way to define a new type by grouping existing types. It groups elements from the same structure for easy creation or reference. Let's look at the example below where we define a struct called Person.

struct Person {
    string name;
    uint age;
    string gender;
}

Array

An array is a data structure that contains a group of elements of the same data type.
In solidity, we have two types of arrays:

  • Fixed-sized array: this is the type of array where the number of elements in the array in known on compile time and cannot be altered.
  • Dynamic-sized array: in this case, the length of the array is unknown at compile time and the length can be altered anytime.

Mapping

A mapping is a data structure that associates a key to a certain value. This makes it possible and easy to access a certain value as long as we have the key. Here is how we define a mapping mapping(keyType => valueType) _name where keyType is a built-in type like string, address but complex types like struct, mapping, or array are not allowed. But valueType can be any type including struct, array or even mapping . Let's take an example of a staking app, you'll need to know which account holds how many tokens. Then to make it easy to get the balance of a certain account ,you'll proceed as follows:

mapping(address => uint256) balances;
address myAddress = "0x28fD274a7D8Bb794076309aEA8D0aa2ae6DF17F1";
uint256 myBalance = balances[myAddress];

// Here "myBalance" will be the balance of "myAddress"

Enum

Enums are used to create a finite set of constant values which you can easily access. Let's say a door has 3 states and you enumerate them as opened, closing, and closed. So in this case you can create an enum which will group them together for easy access.

enum doorStates {opened, closing, closed}

//variable to store a door state called state
doorStates state;

//function to change the state from existing states in doorStates
function setDoorState() public {
        state = doorStates.closed;
}

Wahaaa !!! If you have made it this far, congratulations !

You got a hint on types in solidity and how/where to use them.

Now, go and change the world with your coding skills.

Want to say "Hi"? Reach out to me on twitter @KimMerdi

Wagmi !!!!