Sui Move language - Data Types, Operators

Jay NalamJay Nalam
5 min read

Hello everyone...

Welcome to another day of exploring Web3 Engineer. Today we are starting a new series on Sui Move language. Sui is a layer 1 PoS blockchain and Sui smart contracts are written using Move language. You can read more about Sui blockchain in here.

Move is a object oriented programming language inspired from Rust programming language. It has similar syntax and semantics like Rust and is originally developed for the diem / Libra blockchain by the (formerly Facebook).

We use Sui cli tool for generating sui project management like scaffolding a new project, compiling, testing etc. You can checkout the Sui cli installation guide from the official docs here.

Data Types

In this guide, let us look into the data types in the sui move lang. The primitive data types provided by move lang has

    1. Unsigned integers: Support for unsigned integers of various sizes. Example - u8, u64, u128 etc

      1. Addresses: Unique identifiers used to refer to resources in the Move programming language. It is represented with the keyword address.

      2. Boolean: Boolean value true or false occupying 1 byte of memory.

      3. Bytes: Byte arrays of fixed size are supported by move lang.

      4. Vector <T>: A vector is a dynamic homogeneous collection of type T.

The Sui lang also provide complex data such as:

  1. Structs: Structs are user-defined data structures that allow you to group related data together. They can contain fields of any type, including other structs, resources, and primitive types.

  2. Resources: Resources are unique, non-copyable, and non-droppable values that represent on-chain objects. They are similar to structs but have additional properties that make them suitable for representing assets and other valuable data on the blockchain.

  3. Modules: Modules are collections of related functions, structs, and resources that provide a namespace for your code. They can be used to organize your code and make it more modular and reusable.

  4. Packages: Packages are sets of modules that can be published and shared with other developers. They are similar to libraries in other programming languages and allow you to share your code with others in a reusable and modular way.

  5. Objects: Objects are instances of structs or resources that have been created and stored on the blockchain. They have a unique ID and can be referenced and manipulated by other parts of your code.

  6. Vector: A vector is a dynamic array of items of the same type. It allows you to store a collection of values that can grow or shrink as needed.

  7. Map: A map is a collection of key-value pairs where each key is unique and maps to a value. It allows you to store and retrieve data based on a key, making it useful for implementing associative arrays and other data structures.

Variable declaration

To declare a variable in the Move programming language, we use the let keyword followed by the variable name and an optional assignment of a value. Here is the general syntax for declaring a variable in Move:

let variable_name = value;

If you want to declare a variable without assigning a value immediately, you can simply declare the variable without an assignment:

let variable_name;

Here is an example of declaring a variable x with an initial value of 1 in Move:

let x = 1;

In Move, variables are mutable by default, meaning you can update their values after declaration. Additionally, variable names must follow specific rules: they can contain underscores _, letters a to z, A to Z, and digits 0 to 9, and must start with an underscore _ or a letter a through z.

Operators

The move language supports the below list of operators to facilitate various operations.

  1. Arithmetic operators: Arithmetic operators like +, -, *, /, %, &, |, ^, <<, and >> for mathematical computations.

  2. Comparison operators: Comparison operators like ==, !=, <, <=, >, and >= to compare values and determine their equality or inequality.

  3. Logical operators: Logical operators like && and || are used to perform logical operations on boolean values.

  4. Address operators: Move introduces address operators like address_of and address. These operators are used to manipulate addresses and object IDs.

  5. Mutable reference operator: Move uses the &mut operator to denote mutable references, allowing safe data manipulation and enforcing ownership discipline.

  6. Immutable reference operator: Move uses the & operator to denote immutable references, ensuring safe data manipulation without changing the original data.

  7. Cast operator: For type casting integer types move language provider as operator.

  8. Assert operator: Move has the assert! macro, which acts as an emergency exit when conditions go awry, gracefully halting execution and safeguarding the global state.

  9. Abort operator: Move uses the abort keyword to stop execution and prevent logical errors.

  10. Ternary Operator: The ternary conditional operator, condition ? if_condition_is_true : if_condition_is_false;

Memory allocation

In Sui, memory allocation for smart contract variables is managed through the CARAT compiler and runtime, which inject callbacks to the runtime after every call to an allocation function and maintain an Allocation Table to track all static and dynamic memory allocations and pointers.

In Sui Move, there is no global storage, and objects are stored in Sui memory or Sui storage. The address type is used to represent object IDs, which are globally unique. Objects with key ability have globally unique IDs, and the ID type contains both the object ID and the sequence number.

Stay tuned to learn more about Move language and how to write your contracts on the Sui blockchain. Thank you.

0
Subscribe to my newsletter

Read articles from Jay Nalam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jay Nalam
Jay Nalam

Hi, I'm Jay Nalam, a seasoned Web3 Engineer committed to advancing decentralized technologies. Specializing in EVM-based blockchains, smart contracts, and web3 protocols, I've developed NFTs, DeFi protocols, and more, pushing boundaries in the crypto realm.