What’s the difference between let and var in Mojo?

Mojo is a programming language designed to eventually be a superset of Python. One of its features is the support for scoped runtime value declarations, offering developers flexibility and control over variable mutability. In Mojo, two keywords, let and var, declare variables within a function’s scope. In this Answer, we will examine both of them one by one.

The let keyword

The let keyword is used to declare variables that are immutable, which means that once a value is assigned to a let variable, it cannot be changed. The use of let is particularly useful for:

  • Type-safety: By declaring a variable with let, we ensure that the value remains constant, promoting type-safety. This can help catch errors at compile-time if there is an attempt to reassign the variable.

  • Performance: Immutable variables can be optimized by the compiler for better performance, as the compiler can make assumptions about the variable’s value not changing.

Let’s look at an example:

fn main():
let a: Int = 2 # Declaring 'a' as an immutable variable.
print(a) # Output: 2
a = 2 # This line will result in an error: 'a' is immutable.
print(a)

In the code above:

Line 2: The let a: Int = 2 statement declares a variable a as an immutable integer with an initial value of 2.

Line 3: The print(a) statement prints the value of a, which is 2.

Line 5: The line a = 2 attempts to reassign a new value to a. However, it will result in an error because a was declared as immutable using let. We cannot change the value of an immutable variable.

Line 6: The second print(a) statement will not be executed due to the error in the previous line.

Note: Try removing the 5th and 6th lines of the code to observe how the code works fine without them.

The var keyword

On the other hand, var is used to declare variables that are mutable, meaning their values can be changed. Developers typically use var when they expect the value of a variable to change during the course of a function. The use of var is particularly useful for:

  • Mutability: The var keyword allows us to declare mutable variables, which means we can change their values within the scope of a function. This is valuable when we expect a variable’s value to change during the execution of our program. With var, we can adapt our program's state by updating variable values. This flexibility is especially valuable when working with data that needs to change or evolve over time.

  • Late initialization: We can use var for variables that need to be assigned values later within a function. This is useful when we don't have an initial value for a variable but plan to set it at a later point based on certain conditions.

To fix the previous code we saw, we can use var to remove the error we were facing. Here’s an updated version of our code:

fn main():
var a: Int = 2 # Declaring 'a' as a mutable variable.
print(a) # Output: 2
a = 3 # This line will not throw an error because 'a' is mutable.
print(a)

Note: In our earlier example, we attempted to reassign the same value, "2," to the a variable. However, due to it being declared with let rather than var, we encountered an error. That means that even if we try to reassign the same value to the variable, let never allows reassignment.

When we run the code above, we can see that the value of the a variable has been changed without any errors. This demonstrates how different the two variable types are even though they are used for the same goal.

Also, it's important to note that while let and var are optional within a def function, they are required for all variables in an fn function. Let's take a look at what would happen if we don’t use any of the two keywords in the following snippet:

fn main():
a = 2 # Declaring 'a' without let or var keyword
print(a)

We can see that we get an error message saying that fn declarations require explicit variable declarations and that’s why we got an error.

You can read more about the difference between the fn and def decorations here.

Nevertheless, understanding the distinction between let and var in Mojo allows us to write more predictable, type-safe, and efficient code. We can find a table summarizing what we discussed in this Answer below:

Feature

The let keyword

The var keyword

Mutability

Immutable

Mutable

Type-Safety

Promotes type-safety; compile-time error on reassignment

Allows dynamic changes; no compile-time error on reassignment

Performance

Compiler optimization for better performance

Less optimized due to potential dynamic changes

Use Case

Type-safe scenarios; constant values

Variables expected to change during program execution

Late Initialization

Not suitable for late initialization

Suitable for variables without initial values, set later based on conditions

Conclusion

It's important to recognize that the choice of let as an immutable keyword in Mojo might raise questions and concerns, particularly for developers who are accustomed to popular programming languages like JavaScript. In JavaScript, const is used for immutable variables, while let and var are used for mutable ones. This distinction has become widely accepted in the industry thus it's vital for JavaScript developers transitioning to Mojo to be mindful of this difference and adapt their coding practices accordingly.


Copyright ©2024 Educative, Inc. All rights reserved