DLiteScript

Operators

DLiteScript provides a variety of operators for performing operations on values.

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division10 / 25
%Modulo10 % 31
**Exponentiation2 ** 38

Examples

1
2
3
4
5
6
var sum number = 10 + 5        // 15
var difference number = 10 - 5 // 5
var product number = 10 * 5    // 50
var quotient number = 10 / 5   // 2
var remainder number = 10 % 3  // 1
var power number = 2 ** 3      // 8

String Concatenation

The + operator also works for string concatenation:

1
var greeting string = "Hello, " + "world!" // "Hello, world!"

Array Concatenation

The + operator concatenates arrays as well:

1
2
3
var arr1 []number = [1, 2, 3]
var arr2 []number = [4, 5, 6]
var combined []number = arr1 + arr2 // [1, 2, 3, 4, 5, 6]

Assignment Operators

Assignment operators assign values to variables.

Basic Assignment

OperatorDescriptionExample
=Assignmentx = 5

Compound Assignment

Compound assignment operators perform an operation and assign the result in one step.

OperatorDescriptionExampleEquivalent to
+=Add and assignx += 5x = x + 5
-=Subtract and assignx -= 5x = x - 5
*=Multiply and assignx *= 5x = x * 5
/=Divide and assignx /= 5x = x / 5
%=Modulo and assignx %= 5x = x % 5
**=Exponentiate and assignx **= 2x = x ** 2

Examples

1
2
3
4
5
6
7
8
var x number = 10

x += 5  // x went from 10 to 15
x -= 3  // x went from 15 to 12
x *= 2  // x went from 12 to 24
x /= 4  // x went from 24 to 6
x %= 4  // x went from 6 to 2
x **= 3 // x went from 2 to 8

Comparison Operators

Comparison operators compare two values and return a boolean result.

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than5 > 3true
>=Greater than or equal to5 >= 5true
<Less than3 < 5true
<=Less than or equal to3 <= 5true

Type Comparison

Comparisons between different types are allowed:

1
2
3
4
5
6
printf("%t\n", 1 == 1)           // true
printf("%t\n", "test" == "test") // true
printf("%t\n", null == null)     // true
printf("%t\n", 1 != 2)           // true
printf("%t\n", 1 != "test")      // true (different types)
printf("%t\n", true != "test")   // true (different types)

Logical Operators

Logical operators perform boolean logic operations.

OperatorDescriptionExampleResult
&&Logical ANDtrue && truetrue
||Logical ORtrue || falsetrue
!Logical NOT!falsetrue

Examples

1
2
3
4
5
6
7
8
9
var a bool = true
var b bool = false

printf("%t\n", a && a)  // true
printf("%t\n", a && b)  // false
printf("%t\n", a || b)  // true
printf("%t\n", b || b)  // false
printf("%t\n", !a)      // false
printf("%t\n", !b)      // true

Short-Circuit Evaluation

Logical operators use short-circuit evaluation:

Special Operators

Spread Operator (...)

The spread operator expands array elements, typically used in function calls to pass array elements as individual arguments.

1
2
3
4
5
6
func printThree(a number, b number, c number) {
  printf("%g, %g, %g\n", a, b, c)
}

var numbers []number = [1, 2, 3]
printThree(...numbers) // expands to printThree(1, 2, 3)

Index Operator ([])

The index operator accesses elements in an array by their position (zero-based).

1
2
3
4
5
var fruits []string = ["apple", "banana", "cherry"]

printf("%s\n", fruits[0]) // "apple"
printf("%s\n", fruits[1]) // "banana"
printf("%s\n", fruits[2]) // "cherry"

You can also use it to assign values:

1
2
3
var numbers []number = [1, 2, 3]
numbers[1] = 10
printf("%g\n", numbers[1]) // 10

Operator Precedence

Operators are evaluated in the following order (highest to lowest precedence):

  1. ** (Exponentiation)
  2. ! (Logical NOT)
  3. *, /, % (Multiplication, Division, Modulo)
  4. +, - (Addition, Subtraction)
  5. <, <=, >, >= (Comparison)
  6. ==, != (Equality)
  7. && (Logical AND)
  8. || (Logical OR)

Use parentheses to explicitly control evaluation order:

1
2
var result1 number = 2 + 3 * 4     // 14 (multiplication first)
var result2 number = (2 + 3) * 4   // 20 (parentheses first)
Improve this page