Identifiers may use ASCII letters, numbers, and the underscore, and may not start with a number. A lone underscore is the no-op token, not an identifier.
Immutable declarations use a val token and mutable declarations use a var token.
An immutable declaration must be combined with an assignment.
Mutable declaration without assignment will set a variable implicitly to null.
val x = 123 | immutable declaration and assignment |
var y = 123 | mutable declaration and assignment |
var z | mutable declaration without explicit assignment |
y = 789 | assignment to mutable variable (previously declared) |
y += 789 | assignment to mutable variable using a combination operator |
In statement context, you can assign multiple items from the right to the same number of items on the left. This can be used with val and var declarations.
The result of a multi-variable assignment is the last value on the right.
# swap values x, y = y, x
If you assign a single item from the right to multiple items on the left, it is assumed to be a decoupling assignment. This only works if the value on the right can be indexed numerically.
Use the no-op (underscore) to skip an index.
A decoupling assignment returns a Boolean indicating whether the values could be assigned or not (whether there were enough or not). It will succeed if the number of items on the left is the same or less than the number of items from the right.
# using the fact that submatch() returns an empty list for no match... if alias, name = submatch(row, by=re/(abcd).+(1234)/) { # success (2 or more values in list returned from submatch function) # use alias and name here ... }
If decoupling assignment fails (without throwing an exception) on a declaration, the declared variables will be null.
You can use expansion on the last identifier on decoupling assignment. Upon success, the last variable will be set to a list of 0 or more elements.
if x, y, ...z = somelist { /* do something */ } # z takes all remaining values
System identifiers begin with an underscore and user-defined identifiers do not.
system identifier | description |
---|---|
_rev | the langur revision number string, such as 1.7.3 |
_env | a hash of environment variables with keys and values as strings |
_args | a list of strings of the arguments following the script name |
_file | the name and path of the file being executed |