Skip to content

Require

cyxigo edited this page Jul 13, 2026 · 1 revision

Introduction

Keeping all the code in one file is not a good idea. Programming projects tend to be organized into multiple files. Wi has that ability too - using the require statement.

Usage

Let's imagine there are 2 files: a.wi and b.wi.

// b.wi
var x = 10;
// a.wi

We want that x variable to be used from a.wi. So, we require a b.wi file!

// a.wi
require "b.wi" = b;
print(b.x); // 10

So when we write require "b.wi" = b;, we tell Wi to:

  • Load file b.wi and execute it.
  • Store its global variables into a new global object named b.

require can only be used in top-level code.

It's also worth mentioning that freshly created object b is just like any other variable.

Errors

Wi checks for file existence at compile time:

// a.wi
require "c.wi" = c;

This results in a compile error:

compile error: file c.wi does not exist
   | 
 1 | require "c.wi" = c;
   |         ^^^^^^
   --> a.wi:1:9

Clone this wiki locally