LeekScript 4

LeekScript 4

> LeekScript

LeekScript 4 is the fourth version of the LeekScript language, released on July 1, 2022. This page sums up everything new in this version.

Separate Array and Map

In LeekScript 1 to 3, arrays were both lists ([1, 2, 3, 4]) and associative arrays (['a': 5, 'b': 12]). From LeekScript 4 onward, these two roles are split into two different types: lists (Array) and maps (Map). These two new types perform better than the old shared type, and make code clearer.

An Array list is a continuous sequence of elements, starting at index 0. It is the equivalent of Java's ArrayList or C++'s vector. You can quickly access an element by its position, and insert or remove an element at any position.

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

New list functions

Return of the RAM limit

Since the new lists and maps perform much better and use far fewer operations, a limit on RAM (the program's working memory) had to be brought back. With very few operations, you could otherwise easily fill gigabytes of RAM.

The RAM limit used to be set to 100 MB for each AI (entity + summons) per fight, which is 12,500,000 list, map or object elements. Since update 2.40, it depends on the entity's RAM stat: 8 million 64-bit units (64 MB) per RAM point, up to 50 points. This limit applies in LS4 because the old arrays don't use this RAM system: they don't need it, as they already use up plenty of operations.

The getMaxRAM() and getUsedRAM() functions are added so you can monitor your RAM usage in real time.

64-bit integers instead of 32

Integers are now stored on 64 bits instead of 32 (as in every previous version of LeekScript). This lets you calculate with larger numbers and gives you more bits for bitwise manipulation.

Arrow functions

The "arrow function" syntax is now available, for a simpler way to write anonymous functions:

Default parameters

In constructors, methods and static methods, you can now give each parameter a default value:

List range access and negative indices

New ways to access lists are added:

Change to the == operator, and === deprecated

The == operator, which performed some exotic and potentially confusing equality checks such as 1 == true or 12 == [12], now behaves the same as the === operator, which is deprecated.

Hexadecimal and binary numbers

You can write numbers in hexadecimal and binary, in addition to decimal (thanks to Karang for the PR):

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

"_" number separators

You can write long numeric constants with underscore "_" separators, for example:

var one_billion = 1_000_000_000

Callback argument order

In the arrayMap, arrayFilter, arrayIter, arrayPartition and arrayFoldLeft/Right functions, callbacks always take their arguments in the order (value, key/index). Previously, the order was (key, value) and (value).

On top of that, all these functions pass the array being traversed as the 3rd parameter.

Callbacks can therefore take any number of arguments: the missing values are replaced with null.

Function changes

Basic argument type checking

All the game's functions now have precise types, so errors can be reported when an argument is incompatible with the type a function expects.

Since type inference is still very basic and bound to evolve, these type errors are only warnings for now.

Precedence of a = b = c

You can now write a = b = c without parentheses.

Duplicate key in a map declaration

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

Function complexity in the documentation

The complexity of each function is shown in the documentation.

JSON encoding/decoding of objects and new lists

The JSON functions jsonEncode and jsonDecode now handle the 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.

Number functions

Number constants

final keyword for class fields

You can put the final keyword in front of a class field to indicate that this field can no longer be assigned once the object has been constructed. An assignment means using the = operator: you can still modify the content of the field if it is an object or a list, for example. For a final array 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 aren't already:

Miscellaneous fixes and improvements