LeekScript 4

LeekScript 4

> LeekScript

LeekScript 4 is the fourth version of the LeekScript language, released on July 1, 2022. This page summarizes all the new features brought by this version.

Array and Map separated

In LeekScript versions 1 to 3, arrays were both lists ([1, 2, 3, 4]) and associative tables (['a': 5, 'b': 12]) . As of LeekScript 4, the two aspects are separated into two different types: lists (Array) and tables (Map). These two new types are more efficient than the old common type, and give greater clarity in use.

An Array list is a continuous sequence of elements, starting at index 0. They are equivalent to ArrayList of Java or vector of C++. You can quickly access an element with its position, insert/delete an element at any position.

A Map table is an object that relates a key to a value. They are equivalent to HashMap of Java or unordered_map of C++, the pairs of (key, value) are not ordered. Keys can be of any type: number (integer or real), character string, object, etc. We can quickly retrieve a value associated with a given key. This new type replaces the "associative" aspect of the old arrays

New functions for lists

RAM limit

The new arrays and maps being much more efficient and light in operations, it was necessary to reestablish a limit at the level of the RAM (working memory of the program). Indeed, with very little operation it is possible to fill gigabytes of RAM easily.

The RAM limit is set at 60MB for each AI (entity + summons) per fight, which corresponds to 7,500,000 list, table or object items. This limit is effective in LS4 because the old tables do not use this RAM system, they do not need it because their operation consumption is large enough.

The getMaxRAM() and getUsedRAM() functions are added to monitor its RAM consumption in real time.

64-bit integers instead of 32

Integers are now represented on 64 bits instead of 32 before (in all versions of LeekScript). The increase allows to do calculations with larger numbers and allows to have more bits for binary manipulations.

Arrow functions

The "Arrow function" syntax is now available to write anonymous functions more simply:

Default parameters

In constructors, methods and static methods, it is now possible to use default values for each parameter:

List range access and negative indices

Two new possibilities are added to access lists:

Operator == deprecated and replaced by ===

The old == operator, which did some exotic equality checking like 1 == true. The potentially confusing 12 == [12] is removed in LS4, and replaced with ===, which performs a strict equality comparison. You must therefore write == instead of ===.

Be careful when replacing some of your == by ===, it is possible that the == made a comparison with numbers and/or booleans and/or null.

Hexadecimal and Binary Numbers

It is possible to write numbers in hexadecimal and binary form, in addition to decimal:

var a=0xabcdef // 11259375 var b=0b110110101 // 437

Number separators "_"

It is possible to write long numeric constants with underscore "_" separators, example:

var one_billion = 1_000_000_000

Order of callback arguments

In the arrayMap, arrayFilter, arrayIter, arrayPartition, arrayFoldLeft/Right functions, the callback functions take the arguments (value, key/index) always in this order. The order was (key, value) and (value) previously.

Moreover, all these functions take as 3rd parameter the array being traversed.

It is therefore possible to pass any number of arguments to a callback, the missing values being replaced by null.

Feature Changes

Basic argument type checking

All functions in the game have been precisely typed, which allows errors to be added when a parameter is incompatible with the expected type of a function.

Since type inference is still very basic and destined to evolve, these type errors are for the moment warnings.

Priority of a = b = c

It is now possible to write a = b = c without parentheses.

Duplicate key in table declaration

When declaring a table, an error (LS4+) or a warning is returned if a key appears twice:

Complexity of functions in the documentation

The complexity of each function is displayed in the documentation.

JSON encoding/decoding of objects and new lists

The JSON functions jsonEncode and jsonDecode now handle new lists and objects:

jsonDecode("[1, 2, 3]"): returns a new list jsonEncode("{a: 2, b: 4}") / jsonDecode("{a:2,b:4}"): works with objects.

Functions on numbers

Constants for numbers

final keyword for class fields

It is possible to use the final keyword in front of a class field to indicate that this field is no longer assignable after the construction of the object. An assignment is the use of the = operator, it is always possible to modify the content of the field if it is of the object or list type, for example. For an array-type final field, you must therefore use arrayClear(object.field) instead of object.field = [].

Integer division operators \ and \=

The \ and \= operators are added to perform integer or Euclidean division, for example:

Note that both numbers are converted to integers if they are not already integers:

Miscellaneous fixes and improvements