Behavior of @

Behavior of @

> LeekScript Tutorial

This page is only valid for LS1, in LS2 and more you should no longer use @

In Leekscript, if you haven't noticed yet, there is a limit of operations per turn which is 20 million. Optimization is therefore a key to victory. This article will go into in-depth detail on the behavior of the '@' which is a very popular tool.

Its main use is close to references, that is, copies of value are avoided.

Condensed Explanation

The following explanation serves to give an intuition of the mechanism behind the at sign. It does not constitute an exact technical description because the sources of the version of LeekScript currently used are not available.

0; ""; []; function(x) { return x; };

Assignment

When you create an object directly using a literal (see above), it takes up space in memory. To be able to use it, you have to know where to find it. We will call this position the object's address. When you assign an object to a variable, a copy of that object is made, and then the address is provided to the variable. Instead of providing the address of a copy, we can request that the address of the original object be used, in order to avoid a copy, by prefixing the right-hand expression with the equals sign of a at. (left_hand=@right_hand)

var w = 0; // A copy of 0 is made and the address of this copy is provided. var x =@ 0; // The address of 0 is provided. (Each literal in the code is a different object.) let y = w; // A copy of the copy of 0 is made, and the address of this copy is provided. var z =@w; // The address of the copy of 0 is provided.

The at sign is applied with the highest precedence. So x=@y+z won't do what you might have imagined. The equivalent expression is x = (@x) + y. If you want to assign the address, you will need to add parentheses to specify what the at sign applies to: x =@ (x + y).

Special cases

--- Operations on numbers are not mutating. When you use operators like +=, the result is a new object. This differs from arrays and character strings because the latter two are structures and it is their content that is modified. To make it a little easier to understand how this works, it helps to expand the expressions a bit. x += 1 becomes x = x + 1, we note that there is reassignment. Whereas x += [1] becomes x[count(x)] = 1 or push(x, 1)/pushAll(x, [1]); it is clear that the variable x is not reassigned.

Setting

Similarly, when you pass an object as an argument to a function, a copy is made and the address of this copy is given to the parameter. However, unlike assigning to a variable, prefixing a parameter with an at sign has a slightly more complex behavior when applying the function:

In the rare case that you want to pass a copy rather than the address of the variable, you can prefix the argument with an at sign: at(@zero).

In parameter

Its most stable and practical use is in a function's parameters:

function with_a (@ param1 ) { } // Which we are going to confront with: function whitout_a ( param1 ) { }

Note: If you modify the parameter in the function, this modification will also take place in the variable of the calling program, because the value given is your variable, not the value of it, for example:

function add_1 (@param) { param += 1; }

let a = 5; add_1(a); debug(a); // displays: '6'

Note: Calling a function costs 1 operation. This cost was deducted from the following results.

Passing through raw data Passing through a variable Data passed as parameter without the '@' with the '@' cost of assignment to the variable (cf Assignment) without the ' @' with the '@' 42 2 1 2 2 0 "a string" 2 1 2 2 0 function () {} 2 1 2 2 0 [42, -42, 42] 116 34 116 83 0 ["