Aptos Move by Example
  • 🚀Getting Started
  • Set-Up
  • Why is Move Secure
    • Move prover
  • Move vs Solidity
    • Resources
    • Parallel Processing
    • Reentrancy attacks
    • Memory management
    • Smart contract verification
    • Compiled language
  • Basic Concepts
    • Move.toml
    • Primary data-types
    • Strings
    • Comments
    • Functions
    • Function Visibilities
    • Control flow and expressions
    • Loops
    • Error
    • Struct and its Abilities
    • Scripts
    • Operations
  • Intermediate Concepts
    • Local variables
    • Constants
    • Signer
    • Vector
    • Address
    • Uses and Aliases
    • Maps
    • Hash functions
    • References
    • Unit test
    • Generics
    • Type Arguments
    • Type Inference
  • Advanced Concepts
    • Global Storage Structure
    • Global Storage Operations
    • Phantom Type Parameters
    • Timestamps
    • Ownership
    • Move coding conventions
    • View functions
    • Aptos account
    • Aptos Coin
    • Aptos Token(Nft)
    • Object
    • Token V2
  • Applications
    • First App
    • ToDoList
    • Voting System
    • Basic Tokens
    • Storage using Generics
    • Company
    • Collection
    • Football Card
    • Staking Module
    • MultiSender Wallet
    • English Auction
    • Dutch Auction
    • Attendance Sheet
    • Polling Contract
    • Lottery Contract
  • Decentralized Finance
    • Simple Swap Protocol Contract
    • Code of Swapping Protocol
  • Hacks
    • Coming soon
  • Hands on tutorials
    • Indexer tutorials
Powered by GitBook
On this page
Edit on GitHub
  1. Intermediate Concepts

Constants

Constants are a way of giving a name to shared, static values inside of a module or script. In Move, constants are declared using the const keyword.

The constant's must be known at compilation. The constant's value is stored in the compiled module or script. And each time the constant is used, a new copy of that value is made.

Syntax:

const <NAME>: <TYPE> = <EXPRESSION>;
module my_addrx::Constants
{
    use std::debug::print;

    //Some Examples
    const X:u64=10;
    const Y:address=@my_addrx;
    const Z:bool=false;

    fun constants()
    {
        print(&X);
        print(&Y);
        print(&Z);
        
    }

    #[test]
    fun testing()
    {
        constants();
    }
}

Naming:

Constants must start with a capital letter A to Z. After the first letter, constant names can contain underscores _, letters a to z, letters A to Z, or digits 0 to 9.

//Valid
const Foo:u64=123;
const Flag:bool=true; 
const My_Addrx:address=@my_addrx;

//Invalid;
const x:u8=10;
const flag:bool=false;
const my_addrx:address=@my_addrx;

Some other important points:

  • Constants are limited to the primitive types bool, u8, u16, u32, u64, u128, u256, address, and vector<u8>.

  • Constant names should be upper camel case and begin with an E if they represent error codes (e.g., EIndexOutOfBounds) and upper snake case if they represent a non-error value (e.g., MIN_STAKE).

  • public constants are not currently supported. const values can be used only in the declaring module.

  • Constant value cannot be accessed outside its module or script scope.

PreviousLocal variablesNextSigner

Last updated 2 years ago