forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/AmazingAng/WTFSolidity
- Loading branch information
Showing
8 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
// Return multiple variables | ||
// Named returns | ||
// Destructuring assignments | ||
|
||
contract Return { | ||
// Return multiple variables | ||
function returnMultiple() public pure returns(uint256, bool, uint256[3] memory){ | ||
return(1, true, [uint256(1),2,5]); | ||
} | ||
|
||
// Named returns | ||
function returnNamed() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){ | ||
_number = 2; | ||
_bool = false; | ||
_array = [uint256(3),2,1]; | ||
} | ||
|
||
// Named returns, still supports return | ||
function returnNamed2() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){ | ||
return(1, true, [uint256(1),2,5]); | ||
} | ||
|
||
// Read return values, destructuring assignments | ||
function readReturn() public pure{ | ||
// Read all return values | ||
uint256 _number; | ||
bool _bool; | ||
bool _bool2; | ||
uint256[3] memory _array; | ||
(_number, _bool, _array) = returnNamed(); | ||
|
||
// Read part of return values, destructuring assignments | ||
(, _bool2, ) = returnNamed(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Solidity Minimalist Primer: 4. Function output (return/returns) | ||
|
||
Recently, I have been relearning the Solidity, consolidating the finer details, and also writing a "Solidity Minimalist Primer" for newbies to learn and use from (advanced programmers can find another tutorial). Lectures are updated 1~3 times weekly. | ||
|
||
Everyone is welcomed to follow my Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | ||
|
||
WTF Solidity Discord: [Link](https://discord.gg/5akcruXrsk) | ||
|
||
All codebase and tutorial notes are open source and available on GitHub (At 1024 repo stars, course certification is unlocked. At 2048 repo stars, community NFT is unlocked.): [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity) | ||
|
||
----- | ||
|
||
In this section, we will introduce `Solidity` function output, including returning multiple values, named returns, and reading full and part of return values using destructuring assignments. | ||
|
||
## Return values(return and returns) | ||
There are two keywords about function output: `return` and `returns`, which differ from: | ||
- `returns` is added after the function name to declare variable type and variable name; | ||
- `return` is used for the function body and returns specified variables. | ||
|
||
```solidity | ||
// returning multiple variables | ||
function returnMultiple() public pure returns(uint256, bool, uint256[3] memory){ | ||
return(1, true, [uint256(1),2,5]); | ||
} | ||
``` | ||
In the above code, we stated that the `returnMultiple()` function will have multiple outputs: `returns (uint256, bool, uint256[3] memory) `, and then we determined return values in the function body with `return (1, true, [uint256 (1), 2,5]) `. | ||
|
||
## Named returns | ||
We can indicate the name of the return variables in `returns`, so that the `solidity` automatically initializes these variables, and automatically returns the values of these functions, without adding a `return`. | ||
|
||
```solidity | ||
// named returns | ||
function returnNamed() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){ | ||
_number = 2; | ||
_bool = false; | ||
_array = [uint256(3),2,1]; | ||
} | ||
``` | ||
In the above code, we declare the return variable type and variable name with the `returns (uint256 _number, bool _bool, uint256[3] memory _array) `. This way, we will only need to assign values to the variable ` _ number` in the body, ` _bool ` and ` _array ` and they will automatically return. | ||
|
||
Of course, you can also return variables with `return` in named returns: | ||
```solidity | ||
// Named return, still support return | ||
function returnNamed2() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){ | ||
return(1, true, [uint256(1),2,5]); | ||
} | ||
``` | ||
## Destructuring assignments | ||
`solidity` uses rules for destructuring assignments and supports the full or part of return values of the function. | ||
- Read all return values: declare the variables to be assigned and separate them by `, ` in order. | ||
```solidity | ||
uint256 _number; | ||
bool _bool; | ||
uint256[3] memory _array; | ||
(_number, _bool, _array) = returnNamed(); | ||
``` | ||
- Read part of return values: declare the variables to read in return values and the variables not to read can be left out. In the following code, we only read the return value ` _bool `, but not ` _ number` and ` _array `: | ||
```solidity | ||
(, _bool2, ) = returnNamed(); | ||
``` | ||
|
||
## Verify on Remix | ||
- View the results of the three return methods after deploying the contract | ||
 | ||
|
||
|
||
## Tutorial summary | ||
In this section, we introduced function return values `return` and `returns`, including returning multiple variables, named returns, and reading full and part of return values using destructuring assignments. | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
contract Constant { | ||
// The constant variable must be initialized when declared and cannot be changed after that | ||
uint256 public constant CONSTANT_NUM = 10; | ||
string public constant CONSTANT_STRING = "0xAA"; | ||
bytes public constant CONSTANT_BYTES = "WTF"; | ||
address public constant CONSTANT_ADDRESS = 0x0000000000000000000000000000000000000000; | ||
|
||
// The immutable variable can be initialized in the constructor and cannot be changed after that | ||
uint256 public immutable IMMUTABLE_NUM = 9999999999; | ||
address public immutable IMMUTABLE_ADDRESS; | ||
uint256 public immutable IMMUTABLE_BLOCK; | ||
uint256 public immutable IMMUTABLE_TEST; | ||
|
||
// The immutable variables are initialized with constructor, so that could use | ||
constructor(){ | ||
IMMUTABLE_ADDRESS = address(this); | ||
IMMUTABLE_BLOCK = block.number; | ||
IMMUTABLE_TEST = test(); | ||
} | ||
|
||
function test() public pure returns(uint256){ | ||
uint256 what = 9; | ||
return(what); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Solidity Minimalist Primer: 9. Constant and Immutable | ||
|
||
Recently, I have been relearning the Solidity, consolidating the finer details, and also writing a "Solidity Minimalist Primer" for newbies to learn and use from (advanced programmers can find another tutorial). Lectures are updated 1~3 times weekly. | ||
|
||
Everyone is welcomed to follow my Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | ||
|
||
WTF Solidity Discord: [Link](https://discord.gg/5akcruXrsk) | ||
|
||
All codebase and tutorial notes are open source and available on GitHub (At 1024 repo stars, course certification is unlocked. At 2048 repo stars, community NFT is unlocked.): [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity) | ||
|
||
----- | ||
In this section, we will introduce two keywords in `solidity`, `constant` and `immutable`. After the state variable declares these two keywords, you cannot change the values after the contract and it will help to save ` gas `. In addition, only the numeric variables can declare them as `constant` and `immutable`; the `string` and ` bytes ` can be declared as `constant`, but not as `immutable`. | ||
|
||
## constant and immutable | ||
### constant | ||
The `constant` variable must be initialized when declared and cannot be changed after that. Code cannot compile if try to change it. | ||
``` solidity | ||
// The constant variable must be initialized when declared and cannot be changed after that | ||
uint256 constant CONSTANT_NUM = 10; | ||
string constant CONSTANT_STRING = "0xAA"; | ||
bytes constant CONSTANT_BYTES = "WTF"; | ||
address constant CONSTANT_ADDRESS = 0x0000000000000000000000000000000000000000; | ||
``` | ||
### immutable | ||
The `immutable` variable can be initialized at declaration or in the constructor, so it is more flexible. | ||
``` solidity | ||
// The immutable variable can be initialized in the constructor and cannot be changed later | ||
uint256 public immutable IMMUTABLE_NUM = 9999999999; | ||
address public immutable IMMUTABLE_ADDRESS; | ||
uint256 public immutable IMMUTABLE_BLOCK; | ||
uint256 public immutable IMMUTABLE_TEST; | ||
``` | ||
You can initialize the `immutable` variable using a global variable such as `address(this)`,`block.number`, or a custom function. In the following example, we use the ` test ()` function to initialize the `IMMUTABLE_TEST ` to the ` 9 `: | ||
``` solidity | ||
// The immutable variables are initialized with constructor, so that could use | ||
constructor(){ | ||
IMMUTABLE_ADDRESS = address(this); | ||
IMMUTABLE_BLOCK = block.number; | ||
IMMUTABLE_TEST = test(); | ||
} | ||
function test() public pure returns(uint256){ | ||
uint256 what = 9; | ||
return(what); | ||
} | ||
``` | ||
|
||
## Verify on Remix | ||
1. After the contract deployed, initialized values of the `constant` and `immutable` variables can be obtained through the `getter` function on the remix. | ||
|
||
 | ||
|
||
2. After the `constant` variable initialized, code cannot compile if try to change its value and the compiler will issue an error of `TypeError: Cannot assign to a constant variable. `. | ||
|
||
 | ||
|
||
3. After the `immutable` variable initialized, code cannot compile if try to change its value and the compiler will issue an error of `TypeError: Immutable state variable already initialized. `. | ||
|
||
 | ||
|
||
## Tutorial summary | ||
In this section, we introduced two keywords in `solidity`, `constant` and `immutable`, to keep the variables that should not be changed. It will help to save ` gas ` while improving contract security. | ||
|
||
|