Comments
Walrus supports both single-line and multi-line comments to help you document your code.Single-line comments
Use// for single-line comments:
Multi-line comments
Use/* */ for multi-line comments that can span multiple lines:
Multi-line comments are useful for longer explanations or temporarily disabling blocks of code during development.
Statements and expressions
Walrus distinguishes between statements and expressions:- Statements perform actions (like variable declarations, assignments, function definitions)
- Expressions evaluate to values (like arithmetic operations, function calls, literals)
Statements
Expressions
Semicolons
Walrus does not require semicolons at the end of statements. Each statement is typically on its own line:Whitespace and indentation
While Walrus doesn’t enforce strict indentation rules like Python, proper indentation improves code readability, especially in control structures:Code blocks
Code blocks are delimited by curly braces{ and }:
Identifiers
Identifiers (variable names, function names, etc.) must follow these rules:- Start with a letter (a-z, A-Z) or underscore (
_) - Can contain letters, digits (0-9), and underscores
- Case-sensitive (
myVarandmyvarare different)
Valid identifiers
Invalid identifiers
Reserved keywords
The following words are reserved and cannot be used as identifiers:Control flow
ifelsewhileforinbreakcontinue
Declarations
fnletstructreturnimportas
Literals & operators
truefalsevoidandornot
Print functions
Walrus provides two built-in functions for output:println
Prints a value followed by a newline:Both
print and println automatically convert values to strings for display.Best practices
Use meaningful variable names
Use meaningful variable names
Choose descriptive names that explain the purpose of the variable:
Comment complex logic
Comment complex logic
Add comments to explain why you’re doing something, not what you’re doing:
Keep lines reasonably short
Keep lines reasonably short
Break long expressions into multiple lines for better readability:
Next steps
Data types
Explore Walrus data types including integers, floats, strings, and collections
Variables
Learn how to declare and work with variables