https://docs.soliditylang.org/en/v0.8.10/contracts.html#visibility-and-getters
There are four visibility keywords in Solidity: public
, private
, internal
, and external
.
https://docs.soliditylang.org/en/v0.8.10/types.html#integers
uint
is a datatype. Specifically it is an unsigned integer of 256 bits. It is used for all variables that require an integer. You can think of uint
as a single slot in a database or a single address in a blockchain (Not to be confused with the address
datatype).
uint public tokens = 400;
address
is a 160bit value that does not support arithmetic operations. It is designed to be used for storing addresses of contracts or the public hash or a key to external accounts. A common example of this is the variable of the “caller”. Who is going to be using the contract?
address public minter = msg.sender;
https://docs.soliditylang.org/en/v0.8.10/types.html#booleans
bool
is a datatype that only has two values: true
or false
. bool
stands for boolean. Boolean’s should be used for variables that should only have one of these two states.
bool public state = true;
https://docs.soliditylang.org/en/v0.8.10/types.html#string-literals
string
is a datatype that is made up of ordered sequences of characters. They are often referred to as literals because the contain static data instead of dynamic data provided by a user.
string public helloworld = "Hello World";
In Solidity, however, strings tend to be a bit costly. All transactions in Solidity require fuel which is traded as Ethereum on the blockchain, so you can literally save money by using cheaper data types. An example of this is bytes32
.
bytes32
is a datatype for string literals, but can only contain printable ASCII characters between and including 0x1F to 0x7E. Like strings, bytes can include escape characters. View the link for reference.
bytes32 public helloworld = "Hello World";
A benefit of using a string over bytes is the support of Unicode. Just add the keyword unicode
before the string.
string public helloworld = unicode"Hello World 😀";
https://docs.soliditylang.org/en/v0.8.10/types.html?highlight=array#arrays
In Solidity, arrays can have a fixed size that is assembled in compile time, or they can have a dynamic size. Note that dynamic sized arrays are more costly. Unlike maps, arrays can only contain a single type of data.
To create a fixed sized specify the type of data used within the array followed by the size of the array in square brackets.
uint[5] public myArray;
Dynamic arrays are able to change size after compile time. They are written exactly like fixed-sized arrays but without indicating the size.
uint[] public myArray;
An array literal is a comma-separated list of one or more expressions, enclosed in square brackets. When you define an array with values during the declaration, you are assigning array literals to the array.
uint[5] public myArray = [1, 2, 3, 4, 5];
There are several functions that can be used with arrays:
Push adds one or more elements to the end of an array and returns the new length of the array.
function push(uint number) public {
myArray.push(number);
}
Pop removes the last element from an array and returns that value to the caller.
function pop() public {
myArray.pop();
}
Length is a string property that is used to determine the length of a string.
function getLength() public view returns (uint) {
myArray.length;
}
Delete will remove a value in an array at a specific index, but there is a catch: When the delete is ran, the value is removed, but the size of the array is unchanged, so really delete
is setting a specific index to 0
or null
depending on the data type.
function remove(uint i) public {
delete myArray[i];
}
If you need to remove elements from an array while preserving the integrity of the array, there is an inexpensive way to do that. Keep in mind, that this will not keep the array ordered. To do that, you will require a for loop which would be too expensive if order did not matter.
uint[] public changeArray;
function removeElement(uint[] memory array, uint i) public {
array[i] = array[array.length - 1];
array.pop();
}
You can test the function by pushing some values to changeArray
and then removing an element from it by using the removeElement
function.
for (uint i = 1; i <= 4; i++) {
changeArray.push(i);
}
// changeArray: [1,2,3,4]
removeElement(changeArray, 1);
// changeArray: [1,4,3]
Additionally, to display the values of the array to the console, you can create a getter.
function getChangeArray() public view returns(uint[] memory) {
return changeArray;
}
getChangeArray();