Operators
Operate all things++--==!^%/\
Operators are the foundation of any programming language. Operators are symbols that help a programmer to perform specific mathematical, structuring, destructuring, and logical computations on operands (variables or expressions). We can categorize the BoxLang operators into the following categories:
Arithmetic/Mathematical
Assignment
Logical
Comparison
Ternary
Elvis (Null Coalescing)
Function
Collections
BoxLang does not offer the capability to overload operators like other languages.
Operator Precedence
The order of precedence exists in BoxLang, just like in mathematics. You can also control the order of precedence by using the grouping operator ()
like in mathematics, the magical ordering parenthesis.
Remember that using parenthesis (Grouping Operator)
is very important to denote precedence.
CF code will use CF's precedence.
Arithmetic Operators
These operators are used to perform arithmetic/mathematical operations on operands.
Bitwise Operators
BoxLang has native bitwise operators and it also implements bitwise operations via functions (since functions can also be operators in BoxLang): bitAnd, bitMaskClear, bitMaskRead, bitMaskSet, bitNot, bitOr, bitSHLN, bitSHRN, bitXOR
. You can find much more information here: https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/math
These operators are used to perform bitwise operations on operands.
The bitwise operators look a bit different in BoxLang vs other languages like Java since the normal bitwise operators are already used for other purposes in BoxLang.
For more information about bitwise operations, you can read more here: https://en.wikipedia.org/wiki/Bitwise_operation
Assignment Operators
These operators are usually used for compound evaluations and assignments.
Logical Operators
Logical operators perform logic between values or values, usually denoting a boolean
result.
Comparison Operators
Comparison operators are used when comparing two values, expressions, or variables. The return of a comparison is either true
or false
.
Ternary Operator
The ternary operator is a conditional operator that works just like an if-then-else
statement but in shorthand syntax. It has three operands:
The condition
must evaluate to a Boolean
value. If true
then the value1
will be used, or else value2
will be used. You can combine this operator with parenthesis, Elvis operators, etc., to build rich expressions.
Elvis Operator (Null Coalescing)
The Elvis operator is usually referred to as the null coalescing operator. Its name comes from the symbol it represents, which looks like Elivs hair turned sideways: ?:
. If the expression to the operator's left is null
, then the expression on the right will be evaluated as the result of the expression.
Here is a simple example:
Function Operators
In BoxLang, functions can act as operators as well, as you can use the results of the function call as the operands. Function arguments can also act as expressions, and you can even pass more functions into functions as arguments or even return functions from functions. Now that's a fun tongue twister.
Collections Operators
Many operators can work on collection objects like arrays, structs, and queries. So let's start investigating them.
Safe Navigation Operator
The Safe Navigation operator avoids accessing a key in a structure or a value in an object that does null
or doesn't exist. Typically when you have a reference to an object, you might need to verify that it exists before accessing the methods or properties of the object. To avoid this, the safe navigation operator will return null
instead of throwing an exception, like so:
Spread Operator
Feature coming soon
The spread operator allows an iterable object to expand and merge in certain declarations in code. These objects in BoxLang are mostly arrays and structures. This operator can quickly merge all or parts of an existing array or object into another array or object. This operator is used by leveraging three dots ...
in specific expressions.
You can accomplish the result of the spread operator with the append()
member function or traditional function in a very elegant and user-friendly syntax. It also allows you NOT to do chaining but inline expressions.
The Spread syntax also allows an iterable such as an array expression or string, to be expanded in places where zero or more arguments (for function calls) are expected. Here are some examples to help you understand this operator:
Function Calls
Array Definitions
Struct Definitions
Rest Operator
Only available in ACF 2021+ and for function arguments
The Rest function operator is similar to Spread Operator but behaves oppositely. The spread syntax expands the iterable constructs into individual elements, and the Rest syntax collects and condenses them into a single construct, usually an array. Please note that this operator only works on function arguments as of now.
Imagine I need to create a function that takes in an unlimited number of Identifiers, so I can return all items that have that ID:
You can also combine them in functions with other arguments:
Last updated