DLiteScript

Variables and Constants

DLiteScript supports both mutable variables and immutable constants, both of which require explicit type declarations.

Variable Declaration

Variables are declared using the var keyword followed by a name, type, and optional initial value.

Syntax

1
2
var name type = value
var name type // initialized with zero value

Examples

1
2
3
4
5
6
7
var count number = 42
var message string = "Hello"
var isActive bool = true

var uninitialized number // defaults to 0
var emptyString string // defaults to ""
var defaultBool bool // defaults to false

Reassignment

Variables can be reassigned after declaration:

1
2
3
4
var x number = 10
x = 20
x = x + 5
x += 5

Constant Declaration

Constants are declared using the const keyword. Once initialized, their values cannot be changed.

Syntax

1
const name type = value

Constants must be initialized at declaration. You cannot declare a constant without a value.

Examples

1
2
3
const PI number = 3.14159
const NAME string = "DLiteScript"
const MAX_SIZE number = 100

Immutability

Attempting to reassign a constant results in an error:

1
2
const x number = 10
x = 20 // Error: cannot reassign constant

Scoping

Both variables and constants follow block scoping rules. A variable or constant declared within a block is only accessible within that block and any nested blocks.

Global Scope

Declarations at the top level are accessible throughout the entire file:

1
2
3
4
5
var globalVar string = "accessible everywhere in the file"

{
  printf("%s\n", globalVar) // works
}

Block Scope

Declarations inside blocks are only accessible within that block:

1
2
3
4
5
6
{
  var blockVar number = 10
  printf("%g\n", blockVar) // works
}

printf("%g\n", blockVar) // Error: blockVar not defined

Nested Scopes

Inner blocks can access variables from outer blocks, but not vice versa:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var outer string = "outer"

{
  var inner string = "inner"

  printf("%s\n", outer) // works
  printf("%s\n", inner) // works
}

printf("%s\n", outer) // works
printf("%s\n", inner) // Error: inner not defined

Shadowing

Variables and constants in inner scopes can shadow (hide) names from outer scopes:

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

{
  var x number = 20 // different variable, shadows outer x
  printf("%g\n", x) // prints 20
}

printf("%g\n", x) // prints 10

Type Annotations

All variable and constant declarations must include explicit type annotations. DLiteScript does not perform type inference.

1
2
var count number = 42 // type required
var name = "Alice" // Error: missing type annotation

Naming Conventions

Variable and constant names:

1
2
3
4
5
6
var myVariable number = 1 // valid
var _private string = "ok" // valid
var count123 number = 5 // valid

var 1count number = 5 // Error: cannot start with number
var for number = 1 // Error: 'for' is a keyword
Improve this page