StarknetAstro

StarknetAstro

02_Constants in Cairo

02_Cairo Constants#

The version of the Cairo compiler used in this article is 2.0.0-rc0. Because Cairo is being updated rapidly, the syntax may vary slightly in different versions, and the content of the article will be updated to a 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 indicate the type of the constant and the value of the constant is given 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 like this 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 uses one constant to assign a value to another constant, and the following error will be 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 by default "immutable," which is quite interesting. Because in other mainstream languages, variables are usually declared as mutable by default, while Cairo reverses this. This can be understood as "immutable" usually provides better stability, while mutable brings instability. Therefore, Cairo aims to be a safer language. In addition, Cairo also has constants that can be modified by the const modifier. So, Cairo can do these things:

  • Constants: const LEN:u32 = 1024; where LEN is an unsigned 32-bit integer constant (u32) used at compile time.
  • Mutable variables: let mut x = 5; similar to other languages, used at runtime.
  • Immutable variables: let x = 5; for this type of variable, you cannot modify it. However, 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) are difficult to find. Generally, each variable should have its most appropriate name, and it is best not to have duplicate names.

Advantages of Default Immutability#

Immutable variables are helpful for the stable operation of programs. It is a programming "contract." When dealing with variables that are contracted to be immutable, the program can be much 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 many programming issues at compile time. This is how the Cairo language's compiler can help you detect many programming problems during compilation.

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