Operators
Alia's operators cover arithmetic, logic, null coalescing, conditionals, comparison, parentheses, and string concatenation.
1. Arithmetic Operations
| Operator | Description | Example |
|---|---|---|
+ | Addition | 1 + 2 → 3 |
- | Subtraction | 5 - 3 → 2 |
* | Multiplication | 2 * 3 → 6 |
/ | Division | 6 / 2 → 3 |
% | Remainder | 7 % 3 → 1 |
Division and remainder with a zero divisor trigger a division-by-zero error.
2. Logical Operations
| Operator | Description | Example |
|---|---|---|
& | Logical AND | true & false → false |
| | Logical OR | true | false → true |
! | Logical NOT | !true → false |
Unary - and + can be used as prefix operators, as in -5.
3. Conditionals and Null Coalescing
| Operator | Description | Example |
|---|---|---|
? : | Ternary conditional | (true ? 'yes' : 'no') → 'yes' |
?? | Null coalescing: returns the left value when it is non-null, otherwise returns the right value | (null ?? 'fallback') → 'fallback' |
A ternary conditional whose test bit is Null triggers a type error.
4. Size Comparison
| Operator | Description | Example |
|---|---|---|
> | Greater than | 5 > 3 → true |
< | Less than | 3 < 5 → true |
>= | Greater than or equal to | 5 >= 5 → true |
<= | Less than or equal to | 3 <= 5 → true |
!= | Not equal to | 5 != 3 → true |
== | Equal to | 5 == 5 → true |
The six comparison operators share the same precedence level.
5. Order of Operations
Parentheses () determine the order of operations; expressions inside parentheses are evaluated first.
print (1 + 2) * 3
print 1 + (2 * 3)
The first line evaluates to 9, the second to 7.
6. String Concatenation
The .. operator concatenates its left and right operands and returns a String; numeric operands participate in the concatenation as stringified values. Its precedence is the same as + and -.
print 'Hello' .. ' ' .. 'World' .. '!'
print 1 .. 2
The first line evaluates to 'Hello World!', the second to '12'.
7. Assignment and Increment
= += and ++ are used in assignment and increment statements, not as expression operators.
| Form | Description | Example |
|---|---|---|
let x = v | Declares a variable | let x = 10 |
const x = v | Declares a constant | const x = 10 |
x = v | Assigns to a variable | x = 15 |
x += v | Adds and assigns | x += 5 |
x ++ | Increments by 1 | x ++ |
Example Code
const a = 10
const b = 5
print 'a + b = ' .. (a + b)
print 'a - b = ' .. (a - b)
print 'a * b = ' .. (a * b)
print 'a / b = ' .. (a / b)
print 'a % b = ' .. (a % b)
const c = true
const d = false
print 'c & d = ' .. (c & d)
print 'c | d = ' .. (c | d)
print '!c = ' .. (!c)
print 'a > b = ' .. (a > b)
print 'a < b = ' .. (a < b)
print 'a == b = ' .. (a == b)
print 'a != b = ' .. (a != b)
const age = 18
print (age >= 18 ? 'Adult' : 'Minor')
const myName = 'Alice'
print 'Hello, ' .. myName .. '!'
let x = 10
print 'x = ' .. x
x += 5
print 'x += 5 = ' .. x
x ++
print 'x ++ = ' .. x
const m = null
print m ?? 'fallback'
print (null ?? 'fallback')
print (true ? 'yes' : 'no')
Operator Precedence
Operator precedence in Alia, from highest to lowest:
- Parentheses
() - Unary operators
!-+ - Multiplication and remainder
*/% - Addition and concatenation
+-.. - Comparison
><>=<===!= - Logical AND
& - Logical OR
| - Null coalescing
?? - Conditional operator
? : - Assignment
=
Value Operation Restrictions and Null Handling
An unreduced <Value> triggers the type error TYPE_VALUE_OPERATION when participating in the following operations; narrowing is done via typeOf, Ok, and as:
- Arithmetic operations
+-*/% - Logical operations
&|and unary operations!and unary- - Conditions of
if,while, andfor eachtargets, index accessvalue[...], and property accessvalue.prop- Comparison with another non-literal value:
==!=<><=>=
The following cases are legal dynamic bridges:
- Comparison with literals: both
value == 42and42 == valueare allowed call value.valueOfunwraps the underlying primitive value and returns the original type
.. and ?? do not trigger this restriction: .. always returns a String, while ?? infers its result by merging the left and right types.
Null is a standalone type. Except for ??, any binary or unary operation with Null as an operand errors immediately; a ternary test bit of Null errors; division by zero errors. ?? is the safe path for handling possibly-Null values.
alias process <Value data> -> Value {
if typeOf data == 'Number' {
data * 2
} else {
data
}
}