Created
September 6, 2022 16:25
-
-
Save CJ42/b954c5897a97e1d04894c8e13552df9a to your computer and use it in GitHub Desktop.
Solidity example to show that calldata is read-only and cannot be modified.
This file contains hidden or 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
| pragma solidity ^0.8.0; | |
| contract AllAboutCalldata { | |
| function manipulateMemory(string memory input) public pure returns (string memory) { | |
| // you CAN modify arguments passed with data location 'memory' | |
| // you can add data in the string | |
| input = string.concat(input, " - All About Solidity"); | |
| // you can change the whole string | |
| input = "Changed to -> All About Memory!"; | |
| return input; | |
| } | |
| function manipulateCalldata(string calldata input) external pure returns (string calldata) { | |
| // you CANNOT modify arguments passed with data location 'calldata' | |
| // you cannot add or edit data in the string | |
| // TypeError: Type string memory is not implicitly convertible to expected type string calldata. | |
| input = string.concat(input, " - All About Solidity"); | |
| // you CANNOT change the whole string | |
| // Type literal_string "..." is not implicitly convertible to expected type string calldata. | |
| input = "Cannot change to -> All About Calldata!"; | |
| return input; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment