Skip to main content

Variables and constants

Variable declaration​

Variables are used to store values. The data type of a variable is automatically determined based on the value assigned to it. Therefore, variables in JARU can store values of different types, such as integers, floats, text strings, lists, maps, arrays, etc.

The keyword var in JARU is used for variable declaration. This is one of the ways to create a variable in the language, along with const.

When using var to declare a variable, it only exists inside the block where it's declared: the body of a function, an if, a for, a while, etc. This also applies outside functions: a variable declared inside an if doesn't exist after its end (using it raises the Undefined variable error at runtime). If declared outside any function and any block, it's considered a global variable and can be accessed from anywhere in the code.

Variable declaration with var is done as follows:

var variable = 5

In this example, the variable variable has been declared and assigned the value 5. To assign a value to a variable, use the = symbol followed by the value you want to assign.

It's also possible to declare a variable with var without assigning an initial value. In this case, its value will be 0.

var variable
print(variable) // Prints "0"

As we've seen, in JARU variables automatically acquire the type when assigning a value to them. That is, it's not necessary to specify the variable type at the time of declaration.

Let's look at some examples:

// Declare a variable with a numeric value
var number = 5

// Declare a variable with a text string value
var name = "John"

// Declare a variable with a list value
var list = ["apple", "pear", "banana"]

// Declare a variable with a map value
var map = {"name": "John", "age": 25}

// Declare a variable with an empty array value
var array[10][20]

Text keys in a map go in quotes: in {name: "John"}, name is read as a variable and its value becomes the key.

Names​

The names of variables, constants, functions and classes:

  • Use only unaccented letters, digits and _, and don't start with a digit: lives, _temp, level2, MAX_LIVES.
  • Are case-sensitive: lives and Lives are two different variables. The words of the language are always lowercase (if, end, var).
  • Can't be a reserved word (end, type, on, ref...), and a variable can't be named like a standard library function either (time, len, print, random...).

Accented letters are not allowed in a name, although they are fine inside strings and comments. The compiler gives the error “Unrecognised character in source code”: write the name without accents, as in cafe.

var café = 5
var time = 0    // time is a standard library function

Don't name a global variable like a module either (Display, Math, Sprite...): it compiles, but the program takes it for the module and stops when it uses it, with “Module 'Math' not found or not imported.”.

A function, on the other hand, can be named like one from the standard library, and it hides it in the whole program, also in imported files: after func len(x) … end, len(...) calls yours (until the program reaches that line, it's still the library one). The compiler warns about it ("function 'len' has the same name as a native function"), but it compiles. Give it another name. A method can be named like that (def len() in a class): it's used with the object in front and hides nothing.

Constants​

The keyword const is used to declare a variable as a constant. This means that the variable's value cannot be changed once it has been assigned. The compiler will generate an error if you try to change the value of a variable declared as const. This is useful for ensuring the immutability of important values in a program and for preventing accidental errors.

In JARU, constants are declared using the keyword const at the beginning of the declaration. Once a value has been assigned to a constant, it cannot be changed. If you try to assign a new value to a constant, a compile error will be generated.

const myPI = 3.14
print(myPI)

myPI = 3.15 // generates an error
Important

If a constant refers to an object, list, map, or array, the value of that constant cannot be changed, but the values inside the object, list, map, or array can be changed.

const person = { "name": "Jon" }
println(person["name"]) // shows "Jon" with a line break

person["name"] = "Raquel" // correct assignment

person = { "name": "Steve" } // generates an error because it's a constant

The value of a constant cannot be changed once assigned, but if the constant is an object or array, the values inside that object or array can be changed.

Enumerations (enum)​

enum gives names to a group of fixed values: a character's states, directions, enemy types...

enum State
STANDING, WALKING, JUMPING
end

var state = State.WALKING
println(state) // 1
if (state == State.WALKING) then
println("Walking") // Walking
end
  • Each name is worth its position, starting at 0: STANDING is 0, WALKING 1 and JUMPING 2.
  • They are used with the enumeration's name in front: State.JUMPING.
  • The names are separated by commas or go on separate lines. A short one fits on one line: enum Side LEFT, RIGHT end.
  • With =, a name takes the value you give it, also a string:
enum Note
DO = "C", RE = "D", MI = "E"
end
println(Note.RE) // D
  • After a name with a number, the count carries on from that number: with RED, GREEN = 5, BLUE, BLUE is 6, and with A = -1, B, B is 0. The count never goes down: with A, B, C = 0, D, D is 2.
  • Their values can't be changed: State.STANDING = 5 stops the program with the error “Properties can only be assigned to instances, modules, and abstract objects”.
  • A name that is not in the enumeration, like State.RUNNING, compiles, but the program stops when it gets there: “'State' enum has no property: 'RUNNING'.”.
  • A repeated name doesn't compile: with enum Side LEFT, RIGHT, LEFT end, the error says “the enum 'Side' repeats the name 'LEFT'”.
  • foreach doesn't go through an enumeration: the program stops with “Object is not iterable.”.
  • They work well in a switch, with case State.JUMPING:.

All the known issues of JARU, together, are in Known issues.