Skip to main content

Operators

Alia's operators cover arithmetic, logic, null coalescing, conditionals, comparison, parentheses, and string concatenation.

1. Arithmetic Operations​

OperatorDescriptionExample
+Addition1 + 2 → 3
-Subtraction5 - 3 → 2
*Multiplication2 * 3 → 6
/Division6 / 2 → 3
%Remainder7 % 3 → 1

Division and remainder with a zero divisor trigger a division-by-zero error.

2. Logical Operations​

OperatorDescriptionExample
&Logical ANDtrue & false → false
|Logical ORtrue | false → true
!Logical NOT!true → false

Unary - and + can be used as prefix operators, as in -5.

3. Conditionals and Null Coalescing​

OperatorDescriptionExample
? :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​

OperatorDescriptionExample
>Greater than5 > 3 → true
<Less than3 < 5 → true
>=Greater than or equal to5 >= 5 → true
<=Less than or equal to3 <= 5 → true
!=Not equal to5 != 3 → true
==Equal to5 == 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.

FormDescriptionExample
let x = vDeclares a variablelet x = 10
const x = vDeclares a constantconst x = 10
x = vAssigns to a variablex = 15
x += vAdds and assignsx += 5
x ++Increments by 1x ++

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:

  1. Parentheses ()
  2. Unary operators ! - +
  3. Multiplication and remainder * / %
  4. Addition and concatenation + - ..
  5. Comparison > < >= <= == !=
  6. Logical AND &
  7. Logical OR |
  8. Null coalescing ??
  9. Conditional operator ? :
  10. 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, and for
  • each targets, index access value[...], and property access value.prop
  • Comparison with another non-literal value: == != < > <= >=

The following cases are legal dynamic bridges:

  • Comparison with literals: both value == 42 and 42 == value are allowed
  • call value.valueOf unwraps 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
}
}

Next Steps​