Understanding strict mode warnings

Understanding strict mode warnings

> LeekScript Tutorial

You've decided to switch to strict mode, and the editor is now flooding you with warnings that weren't there before. Don't panic: that's the whole point of strict mode. It is less permissive and gives you less freedom, especially when it comes to types. Let's go through a few common warnings, first to understand what problem each one is reporting, then to find ways to fix it.

Dangerous conversion from type 'real' to 'integer'

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


integer b = sqrt(4) //Dangerous conversion from 'sqrt(4)' of type 'real' to 'b' of type 'integer'.

The problem here is that the sqrt function always returns a real value, even though mathematically sqrt(4) equals 2. A real number doesn't fit into an integer variable without losing something: what would happen to the decimals of sqrt(2), for example? So the compiler asks you to make an explicit choice. There are several ways to get rid of this warning:


integer b = floor(sqrt(4)) //Explicitly round down to the integer below: the result really is an integer
integer b = round(sqrt(4)) //Or round to the nearest integer
real b = sqrt(4) //Declare the variable as real, so it accepts the value without any conversion

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

Another case: I have a map (Map) of integers defined as follows, and I want to store one of its values in a variable that is also an integer. The types should match, right?


Map<integer, integer> intMap = [0:3]
integer a = intMap[0] //Dangerous conversion from 'intMap[0]' of type 'integer?' to 'a' of type 'integer'.

Why does the warning say that intMap[0] is of type 'integer?' and not simply 'integer'? As a reminder, the '?' at the end of a type means that the type also accepts null values: it is equivalent to 'integer | null'. So why could my map return null, when according to its type it only contains integers? To read a value from a map, you have to give it a key, here '0' in brackets. That key must be of the key type declared for the map, here an integer, so everything is fine on that side. But nothing guarantees to the editor that the key actually exists in the map, and reading a map with a key that doesn't exist always returns null. So reading a map by its key always gives a nullable result. Now let's see how to solve this problem and get rid of the warnings:


integer a = intMap[0]! //If you know in advance that the key is always in the map, the ! removes the nullable type
integer a = mapGet(intMap, 0, 0) as integer //Use the mapGet function, which can return a default value when the key doesn't exist
integer? a = intMap[0] //Accept the integer? type

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

Here, I have a list of integers that I want to sort. Nothing complicated at first glance; I'm even reusing the example from the documentation


Array<integer> sorted = arraySort([4, 1, 2, 5, 3], (a, b) => a - b) // [1, 2, 3, 4, 5] 
//Dangerous conversion from '(Array<integer>, Function<any, any => any>)' to '()'
//Dangerous conversion from 'arraySort([4, 1, 2, 5, 3], function(a, b) { return a - b; } )' of type 'Array' to 'sorted' of type 'Array<integer>'.

And yet I get 2 warnings on this one simple line. Let's start with the first one: it is wrapped in parentheses, which means the problem lies in the function's arguments, which don't match the expected signature. Let's look up arraySort in the documentation to see its signature: Function real | integer> => Array> So it is a function that expects 2 parameters: a list as the first parameter, and as the second parameter a function that also takes 2 parameters and returns an integer or a real. We do pass a list as the first argument, no problem there, so the second argument is the one that's wrong: we pass a function whose signature is Function any>, whereas arraySort expects Function real | integer>. The return type isn't the expected one.


Array<integer> sorted = arraySort([4, 1, 2, 5, 3], (integer a, integer b) => integer a - b) // [1, 2, 3, 4, 5] 
//Dangerous conversion from 'arraySort([4, 1, 2, 5, 3], function(a, b) { return a - b; } )' of type 'Array' to 'sorted' of type 'Array<integer>'.

The typing problem with the callback function is solved; now only the second warning is left. As we just saw, arraySort doesn't return an Array\, which is what our variable expects, but simply an Array, so we need to cast the return value explicitly to fix the problem.


Array<integer> sorted = arraySort([4, 1, 2, 5, 3], (integer a, integer b) => integer a - b) as Array<integer> // [1, 2, 3, 4, 5]