Water basics

Water is a strongly typed object-oriented programming language. It draws inspiration from both low-level languages like C/C++, but also languages like Typescript.

Hello world example

This example Water program prints "Hello world!" to the console. It achieves this by importing the "ConsoleLog" function from its outer Javascript environment, and exporting a "Main" function to Javascript which uses this function to log the string to the console.

To start off, I should clarify that the editor embedded in this website does a little bit more work behind the scenes. As you might be aware, WebAssembly (which Water gets compiled to) must be instantiated from Javascript. This is where the aforementioned importing and exporting is made use of. You can see a full example, including the Javascript instantiation code, here.

With that out of the way, the main takeaway here is that Water programs are fully sandboxed from Javascript, and that they can only communicate via imports and exports.

Module imports define the signatures of the functions which are getting imported. In this case, the ConsoleLog function has a single `string*` argument. The `*` after `string` means that this is a pointer to a string. The imported function returns nothing, so its return type is `void`.

Imports are always namespaced two layers deep (this is just how it is standardised in WebAssembly). If you look at the example linked above, you will see that the import object in Javascript contains a function entry at the `console.log` path.

Then, the exported "Main" function simply calls this function, which gets displayed on the console. Again, the editor takes care of calling the "Main" function, but you can see how this is done from the Javascript side in the example.