Finn

Finn

👨🏻‍💻工作:Web3 产品经理 🏙️城市:香港 🧩性格:INFP 最近在做 GameFi,能赚钱的游戏
jike
email

Move on Aptos Spring Training Camp Notes | 01 & 02

02 Move Language and Variables#

Variables#

  • Variables: named objects;

  • Objects: memory regions that store values of a certain type; (similar to pointers in C++)

  • Values: collections of bytes interpreted according to their type;

  • Types: define the possible values and operations

Storage#

  • Value types

    • Stored on the stack

    • Simple types: boolean, integer, floating point...

  • Reference types

    • Stored on the heap

    • Complex types: objects, arrays, classes, functions...

When we define a variable as an object, its value is stored on the heap, and the index of its heap memory is stored on the stack, with the index on the stack pointing to the reference type data stored on the heap. (Variable points to object)

Core Differences in Move#

In terms of the relationship between value types and reference types, it differs from other languages in terms of indexing. Instead, it uses "ownership" as the operation for reference types.

  1. Ownership can only be held by one "person" at a time;

  2. Ownership can be transferred to other variables;

For example:

B = A;

C = B;

At this point, B is empty, and C points to A.

Benefits of this approach:

  1. Avoids memory leaks, unlike other languages, memory can be reclaimed at compile time;

  2. Improves memory usage efficiency by avoiding unnecessary copying of reference types;

  3. Better readability and maintainability;

03 The Four Major Types: uint, String, Bool, address#

uint Type#

Multiple ways to write it

#[test]
    fun test_num(){
        let num_u8: u8 = 42; // 0-2**8-1
        let num_u8_2: u8 = 43u8;
        let num_u8_3: u8 = 0x2A;//hash
        let num_u256:u256 = 100_000;
        let num_sum: u256 = (num_u8 as u256) + num_u256;
        print(&num_u8); //42
        print(&num_u8_2); //43
        print(&num_u8_3); // 42
        print(&num_u256); //100000
    }

Bool Type#

#[test]
    fun test_bool(){
        let bool_true: bool = true;
        let bool_false: bool = false;
        print(&bool_true); //true
        print(&bool_false); //false
        print(&(bool_true == bool_false)); //false
    }

String Type#

#[test]
    fun test_string(){
        let str:String = utf8(b"hello world");
        print(&str); //"hello world"
    }

address Type#

The output is @0×2a, but zeros are automatically padded in between.

#[test]
    fun test_addr(){
        let addr:address = @0x2A;
        let addr2:address = @0x000000000000000000000000000000000000000000000A;
        print(&addr); //@0x2a -> @0x(0000...)2a
        print(&addr2); //@xa
    }
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.