Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Constant Keyword
  • Introduction to the immutable Keyword
  1. STEP 2

10: Immunable Keyword

Constant Keyword

The constant keyword is used to declare a state variable as a constant. It is important to note that a constant can only be assigned a value during its declaration and cannot be changed afterwards.

A constant can also be declared with the view or pure keyword, which allows functions to access the constant without changing its value.

constant in function scoop was deprecated.

Example:

contract MyContract {    
    uint constant tokenTotalSupply = 100;     

    function getTokenTotalSupply() public view returns (uint) {        
        return tokenTotalSupply;    
    }
}

In the above example, tokenTotalSupply is declared as a constant and its value is assigned to 100. The function getTokenTotalSupply() is declared as public view and allows external contracts to view the constant without changing its value.

Introduction to the immutable Keyword

The immutable keyword is a powerful tool in Solidity that allows developers to create data that is protected from tampering. It is a way to ensure that stored data is safe and secure, and that it cannot be changed without explicit authorization.

  • Example #1

Consider the following code:

contract MyContract {    
    uint256 public immutable myNumber; 

    function setMyNumber(uint256 _myNumber) public {        
        myNumber = _myNumber;    
    }
}

In this example, the immutable keyword is used to declare a public variable myNumber. This means that the value of myNumber can be read by anyone, but it cannot be changed without using the setMyNumber function.

  • Example #2

Now consider the following code:

contract MyContract {    
    bytes32 public immutable myData; 

    function setMyData(bytes32 _myData) public {        
        myData = _myData;    
    }
}

In this example, the immutable keyword is used to declare a public variable myData. This means that the value of myData can be read by anyone, but it cannot be changed without using the setMyData function.

Conclusion

The immutable keyword is a powerful tool in Solidity that allows developers to create data that is protected from tampering. By using this keyword, developers can ensure that their data is safe and secure and cannot be changed without explicit authorization.

That's it for the lesson 10! In the next lesson, Event

Previous9: Pure KeywordNext11: Events

Last updated 1 year ago