Understanding strict mode warnings

Understanding strict mode warnings

> LeekScript Tutorial

You have decided to switch to strict mode, and the editor is flooding you with warnings that did not exist before. Don't panic: that is the whole point of strict mode, being less permissive and leaving less freedom, especially about typing. Let's look at a few common warnings, to understand what the reported problem actually is, and find ways to fix it.

Dangerous conversion from type 'real' to 'integer'

A first simple case: I want to store the result of a computation in a variable of integer type.

The problem here is that the sqrt function always returns a value of real type, even if mathematically sqrt(4) equals 2. But a real number does not fit without loss into a variable of integer type: what should be done with the decimals of sqrt(2), for instance? The compiler therefore asks you to make an explicit choice. Several solutions exist to fix this warning:

Dangerous conversion from type 'integer?' to 'integer'

Another case: I have an associative array (Map) of integers defined as follows, and I want to store a value from this map in a variable of integer type as well; normally the types should match...

Why does the warning tell us that mapEntier[0] is of type 'integer?' and not simply 'integer'? As a reminder, the '?' at the end of a type means that this type also accepts null values; it is equivalent to 'integer | null'. So why could my map return a null value, when it only contains integers according to its type? When you want to read a value from a map you have to give it a key, here '0' passed between brackets; this key must be of the type defined in the map, here an integer, so all is well on that side. But nothing guarantees to the editor that the key exists in the map, and if you read a map with a key that does not exist, the result will always be null. So the result of accessing a map by key will always be nullable. Now let's see how to solve this problem to get rid of the warnings:

Dangerous conversion from type '(Array, Function any>)' to '()'

In this case, I have a list of integers that I want to sort; nothing complex at first sight, I even reuse the example from the documentation

Yet I end up with 2 warnings on this single line. Let's start with the first one: it is surrounded by parentheses, which means the problem lies with the function parameters, which do not match the expected signature. Let's look at the arraySort documentation to see its signature: Function real | integer> => Array> So it is a function that expects 2 parameters: a list as first parameter, and as second parameter a function that also takes 2 parameters and returns an integer or a real. As first parameter we do pass a list, no problem there, so it is the second parameter that is wrong: we pass a function with signature Function any>, whereas arraySort expects Function real | integer>; the return type is not the expected one.

The typing problem of the callback function is solved; the second warning remains. As seen just before, the return value of the arraySort function is not an Array\ as expected for our variable, but simply Array, so the return value must be explicitly cast to fix this problem.