Includes
Sometimes, when your project gets more complicated, you will find you need to separate it into multiple files. Wing gives you the option to include (import) other Wing files in your code by adding a line with
#include
followed by the file name, like this:#include /path/to/another/file.wing
If you wish to include a JavaScript file using the Wing API features (used for binding), you need the name of JavaScript file you want to include to end with
.wjs
:#include /path/to/a/javascript/file.wjs
When you include another file, it will behave just like the file you included is part of the file you include it from. Any code it contains will run immediately.
/code/supercool/more.wing
function dostuff
print Hello I am doing something
end
print Hi, this is imported
/code/supercool/main.wing
#include /code/supercool/more.wing
print Hello
dostuff
The code above outputs
Hi, this is imported
, followed by Hello
, then followed by Hello I am doing something
; this means the more.wing file is executed as if it was part of the main.wing file from the point the #include
line is.Wing comes with a number of built-in modules, that need to be included before they can be used. These modules provide advanced functionality that isn't part of Wing by default.
To include a built-in module, you need to wrap its name in
<
and >
:#include <filesystem>
Alternatively, if you do not wish to use special characters, you can prefix the name with
wing:
:#include wing:filesystem
If the module cannot be found, the same will happen as if the module was external.
There are a few limitations and peculiarities to including files in Wing, that makes it different from other languages.
For consistancy purposes, you cannot use
#include
in a function:function stuff
#include /some/path/to/a/file.wing -- This lines gives an error
something
end
The code above gives an "Attempted to import from a function context" error. Instead, it should be replaced with:
#include /some/path/to/a/file.wing
function stuff
something
end
To make it simpler, you can assume
#include
can only be placed on the very top of the file (although that is not actually the case).By default
#include
will only give an error when the requested file cannot be included, and will continue running any code below it. For example:#include /this/file/does/not/exist.wing -- This gives an error
print Hello -- This is executed
This may not be what you want, especially if your code depends heavily on externally included code (you don't want to get hundreds of errors just because of a failed include). To solve this, you can use
#require
instead of #include
.Using
#require
instead of #include
makes the included file a requirement, meaning the code will not continue running if, for any reason, the requested file cannot be included:#require /this/file/does/not/exist.wing -- This gives an error and exits the program
print Hello -- This is not executed
Enabling strict mode makes
#include
and #require
have the same behavior, since both will cause the program to exit on any error.Last modified 3mo ago