Mojo’s Syntax
Learn: Syntax and semantics
Mojo's key advantage is its easy-to-understand syntax. Here are some essential aspects to remember:
- Mojo uses all of Python’s syntax and semantics:
- Mojo uses line breaks and indentation to define code blocks.
- No curly braces.
- Supports all of Python’s control-flow syntax such as if conditions and for loops.
- Keywords: Many of the keywords in Mojo are the same as the keywords in Python. For example, the print() function is used to print output in both Mojo and Python.
- Operators: The operators in Mojo are also the same as the operators in Python. For example, the + operator is used to add two numbers in both Mojo and Python.
- Mojo already includes many features and capabilities beyond what’s available in Python.
Mojo: variables
Mutable Variables: Mutable variables are those whose values can be changed after creation. This means you can modify the value of a mutable variable without creating another instance or object.
Immutable Variables: Immutable variables, on the other hand, cannot be changed once created. This means that you cannot modify the value of an immutable variable; instead, if you need to make changes, you have to create another instance or object with the desired value.
Mojo variables are declared using the let or var keywords. The “var” keyword indicates that the variable is mutable , while the “let” keyword indicates that the variable is immutable.
Here is an example of a Mojo variable declaration with the var keyword:
var x = 100
This declaration creates a mutable variable named x and initializes it to the value 100
Example (mutable variable):
var x: Int = 100
print(x)
x += 10
print(x)
Output:
100 110
Here is an example of a Mojo variable declaration with the let keyword:
let y = 200
This declaration creates an immutable variable named y and initializes it to the value 200.
Example (immutable variable):
let y: Int = 200
print(y)
y += 20
print(y)
Output:
error: Expression [8]:20:5: expression must be mutable for in-place operator destination y += 20 ^ expression failed to parse (no further compiler diagnostics)
That's because let makes it immutable, so you can't increment the value.
Mojo provides scoped runtime value declarations: let is immutable, and var is mutable. These values use lexical scooping and support name shadowing:
Example:
def test(x, y):
let z = x
if z != y:
let r = y
print(r)
test(200, 600)
Output:
600
let and var declarations support type specifiers as well as patterns, and late initialization:
Example:
def test():
let x: Int = 100
let y: Float64 = 55.23
let z: Float32
if x != 0:
z = 1.0
else:
z = foo()
print(z)
def foo() -> Float32:
return 3.14
test()
Output:
1.0
Mojo variables can be of any type, including integers, floats, strings, and structs.
Mojo struct variable
A Mojo struct variable is a type of variable in the Mojo programming language used to store a collection of data. A struct in Mojo is similar to a class in Python: they both support methods, fields, operator overloading, decorators for meta programming, etc. The structure and contents of a Mojo struct variable are set in advance and cannot be changed while the program runs. As a result, Mojo struct variables are very efficient, since the compiler knows exactly how much memory they will require.
Note: Mojo will also support classes in the future.
Mojo struct variables are declared using the struct keyword. Mojo struct variables can be accessed using the dot notation. The following code declares a Mojo struct variable called my_struct:
struct my_struct:
var first: Int
var second: Int
fn __init__(inout self, first: Int, second: Int):
self.first = first
self.second = second
let p = my_struct(100, 200)
print(p.first);
print(p.second)
Output:
100 200
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/mojo/mojo-syntax.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics