> 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.
A simple first case: I want to store the result of a calculation in an integer variable.
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:
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?
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:
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
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.
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.
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.