-
Notifications
You must be signed in to change notification settings - Fork 0
Optimizing module imports
MonkeyC supports importing modules via import and using declarations. This has two benefits. First, it makes the names shorter, and easier to type. But it can also make the code shorter and faster.
For example, you could write
import Toybox.Lang;
function getConfig(name as String) as Object {
return Toybox.Application.Properties.getValue(name);
}
Or you could import Toybox.Application:
import Toybox.Lang;
import Toybox.Application;
function getConfig(name as String) as Object {
return Application.Properties.getValue(name);
}
And the code is shorter to type, generates less byte code, and runs faster. An all round win.
In fact, as of sdk-4.1.6, import also makes all the classes and modules in the imported module visible. So you could write
import Toybox.Lang;
import Toybox.Application;
function getConfig(name as String) as Object {
return Properties.getValue(name);
}
but this is just syntactic sugar. The generated code is exactly the same between these last two. But if you now also add an import of Toybox.Application.Properties, the code gets even smaller (and faster).
import Toybox.Lang;
import Toybox.Application.Properties;
function getConfig(name as String) as Object {
return Properties.getValue(name);
}
This also works with your own modules. If you declare a module named MyModule, adding import MyModule will make the code smaller and faster, even though you still have to write MyModule.<whatever> everywhere.
When this option is selected, the optimizer will attempt to import everything you use, in order to reduce code size, and improve the speed of your code.