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

Local variables

Local variables in Move are lexically (statically) scoped. New variables are introduced with the keyword let, which will shadow any previous local with the same name. Locals are mutable and can be updated both directly and via a mutable reference.

Syntax:

let <VARIABLE>:TYPE;  //explicit type annotation
let <VARIABLE>=<EXPRESSION>;  //implicit type annotation
module my_addrx::Variable
{
    fun local_variables()
    {
        let b:u8;
        let c=false;
        let d=b"hello world";
        let e:u64=10000;
        let f:address = @my_addrx;
    }
}

Variables must be assigned before use

Move's type system prevents a local variable from being used before it has been assigned.

let a;
let b=a+10; //ERROR

Valid variable names

Variable names can contain underscores _, letters a to z, letters A to Z, and digits 0 to 9. Variable names must start with either an underscore _ or a letter a through z. They cannot start with uppercase letters.

// all valid
let x = 1;
let _x = 1;
let _A = 1;
let x0 = 1;
let xA = 1;
let foobar_123 = 1;

// all invalid
let X = 1; // ERROR!
let Foo = 1; // ERROR!

Multiple declarations with tuples

let can introduce more than one local at a time using tuples. The locals declared inside the parenthesis are initialized to the corresponding values from the tuple.

//Valid
let (x,y,z) = (1,@0x1,false);
//Invalid
let (p,q) = (1,2,3,4);

Underscore for unused variables

In Move every variable must be used (otherwise your code won't compile), hence you can't initialize one and leave it untouched. Though you have one way to mark variable as intentionally unused - by using underscore _.

module my_addrx::Variable
{
    fun local_variables()
    {
        let a=1;   //compiling this module will result in error as "a" is not being used.
    }
}
module my_addrx::Variable
{
    fun local_variables()
    {
        let _=1; //compiling this module will not result in error
        
        let b=10;
        _=b;    //This is called shadowing
    }
}
PreviousIntermediate ConceptsNextConstants

Last updated 2 years ago