StarknetAstro

StarknetAstro

02_Cairo Constants

Constants in Cairo#

The version of the Cairo compiler used in this article is 2.0.0-rc0. Since Cairo is being updated rapidly, there may be slight differences in syntax between different versions, and the article will be updated to the stable version in the future.

Basic Usage#

use debug::PrintTrait;

const ONE_HOUR_IN_SECONDS: felt252 = 3600;

fn main(){
    ONE_HOUR_IN_SECONDS.print();
}

The const keyword is used to declare a constant and specify its type, and the value of the constant is provided at the end.

Difference from Immutable Variables#

Constants have the following properties:

  1. The mut keyword is not allowed.
  2. They can only be declared in the global scope.
  3. Only literals can be used to assign values to constants.

Let's try declaring a constant inside a function:

use debug::PrintTrait;

fn main(){
    const ONE_HOUR_IN_SECONDS: felt252 = 3600;
    ONE_HOUR_IN_SECONDS.print();
}

Writing it this way will result in a lot of errors 🙅.

Using a non-literal value for assignment will also result in an error:

use debug::PrintTrait;

const TEST: felt252 = 3600;
const ONE_HOUR_IN_SECONDS: felt252 = TEST;

fn main(){
    ONE_HOUR_IN_SECONDS.print();
}

The above code attempts to assign one constant to another, and the following error is received:

error: Only literal constants are currently supported.
 --> d_const.cairo:4:38
const ONE_HOUR_IN_SECONDS: felt252 = test;
                                     ^**^

Summary of Variables and Constants#

In Cairo, variable declarations are immutable by default, which is quite interesting. In other mainstream languages, variables are usually mutable by default, but Cairo does the opposite. This can be understood as immutability generally providing better stability, while mutability can introduce instability. Therefore, Cairo aims to be a safer language. Additionally, Cairo also has constants that can be modified using the const modifier. As a result, Cairo can do the following:

  • Constants: const LEN:u32 = 1024; where LEN is a constant of type u32 (unsigned 32-bit integer) used at compile time.
  • Mutable variables: let mut x = 5; which is similar to other languages and used at runtime.
  • Immutable variables: let x = 5; You cannot modify this type of variable, but you can redefine a new x using the syntax let x = x + 10;. This is called shadowing in Cairo, where the second x shadows the first x.

Using shadowing in Cairo can be troublesome. Bugs caused by using variables with the same name (in nested scope environments) can be difficult to find. Generally, each variable should have its most appropriate name, and it is best to avoid naming conflicts.

Advantages of Default Immutability#

Immutable variables are helpful for the stable operation of a program. It is a programming "contract" that when dealing with immutable variables, the program can be more stable, especially in multi-threaded environments, because immutability means read-only.

Other benefits include being easier to understand and reason about compared to mutable objects, and providing higher security. With such a "contract," the compiler can easily check for many programming errors at compile time. This is how the Cairo language's compiler can help you detect many programming issues during compilation.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.