Basic Syntax#
No More Forced Indents with Jac#
Jac loves Python's syntax but isn't a fan of the forced indentation for defining scopes. Jac introduces curly braces {}
and semicolons ;
for statement terminators (similar to most other languages in existance).
In Python, indentation determines the beginning and end of blocks, which we observe being problematic in many places. Jac retains Python's readability while eliminating issues associated with whitespace.
Basic Statement#
Every statement in Jac ends with a semicolon ;
.
Variables#
Variable declaration and assignment remain identical to Python though its not the line break that signifies the end of a statement, it's the semicolon.
Control Structures#
Control structures in Jac are defined using curly braces {}
and not indentation.
If-else#
And you can also do
or
or even
Loops#
For loops and while loops:
Functions#
Function definitions utilize curly braces {}
to enclose the function body.
Classes#
Class definitions and methods also follow the curly brace convention.
object Rectangle {
has width: float, height:float;
can area(self) {
return self.width * self.height;
}
}
can start {
rect = Rectangle(10, 5);
print(rect.area());
}
Exception Handling#
Similar to Python, you can handle exceptions using try
, except
, else
, and finally
. However, in Jac, you'd surround these blocks with curly braces.
try {
# Intentional error for demonstration
result = 10 / 0;
} except ZeroDivisionError {
print("Cannot divide by zero");
} else {
print("Division successful");
} finally {
print("This will execute no matter what");
}
Migrating from Python#
If you're transitioning from Python, remember the primary changes:
- Curly Braces
{}
: Use them to define blocks of code. - Semicolons
;
: End each statement with them.
Jac provides this fresh approach for developers who appreciate Python's syntax but prefer braces and semicolons for defining scope.