In this article, I will explain how to use mappings inside structs in Solidity.
This guide will explain what are mappings in Solidity and how to use them structs. We will also learn what structs are in the world of Solidity. Let us get started with our guide right away.
What are Mappings in Solidity?
We will start by learning what mappings mean in Solidity.
Mappings are similar to hash tables that help store some data in them. They store data in the key-value pair format somewhat like objects in JavaScript or dictionaries in Python.
In Solidity, we call it a mapping. Like every other variable type, we must declare with the type in Solidity. Here’s what a hash table looks like, thanks to Upstate Interactive on Medium for the diagram:
The syntax of mapping in Solidity looks like this:
mapping(_KeyType => _ValueType) public mappingName
Here’s an example of mapping in Solidity, again by Upstate Interactive on Medium:
Mappings are used to contain information in the form of key-value pairs, where the key can be any of the built-in data types but not reference types, and the value can be any type. Mappings are commonly used to link the unique Ethereum address to the associated value type.
Creating a Mapping in Solidity
Now that we understand what mapping is, let us now create a mapping by ourselves. Well, defining a mapping isn’t a long-step method. Let us define our mapping anyway:
mapping(address => uint) public playerLevel;
Yep. That’s it! Here are a few things to note about mappings:
- Mappings are useful because they can hold a large number of _KeyTypes to _ValueTypes.
- Mappings don’t really have a length, nor do they have the concept of setting a key or a value.
- Mappings are only applicable to state variables that serve as storage reference types.
- When mappings are initialized, they contain every possible key and are mapped to values whose byte-representations are all zeros.
What is a Struct in Solidity?
Solidity gives users the ability to create their own data types in the form of structures. The struct contains a collection of elements, each of which has a different data type. In most cases, it is used to represent a record. The struct keyword is used to define a structure, which creates a new data type.
This is what the syntax of structs in Solidity looks like:
struct <structure_name> {
<data type> variable_1;
<data type> variable_2;
}
To access any element of the structure, we use the dot operator, which separates the struct variable from the element we want to access. The variable of structure data type structure name is used to define it.
Creating a Struct in Solidity
Let us learn how to create a struct by ourselves.
contract test {
// Declaring a structure
struct Ogre {
string name;
string family;
uint level;
bool dead;
}
}
How to Use Mappings Inside Structs in Solidity
Now let us get to the point. We will now simply declare mapping inside a struct and create a function to set values to the struct.
contract Crowdfund {
// defining our Request struct
struct Request {
string description;
uint256 value;
address recipient;
bool complete;
uint256 approvalCount;
mapping(address => bool) approvals;
}
function createRequest(
string description,
uint256 value,
address recipient
) public ownerOnly {
Request memory newRequest = Request({
description: description,
value: value,
recipient: recipient,
complete: false,
approvalCount: 0
});
// Request(description, value, recipient, false); // alternative syntax to create Request instance - NOT RECOMMENDED!
requests.push(newRequest);
}
}
Conclusion
Learn to use mappings inside structs in Solidity.
Noteworthy References
https://stackoverflow.com/questions/66072737/use-mappings-inside-structs-in-solidity