When we run:
valec build mymodule=~/myprogram.vale --output_dir build
the mymodule=~/myprogram.vale is adding ~/myprogram.vale to a new module named mymodule. A module is a grouping of source code. 0 We can also:
Either way, all our code will be in one big mymodule module. By default, code in a module can use any other code defined in the same module.
To use a library, it must be somewhere on your disk. Usually, this means cloning it from github, like so:
git clone https://github.com/ValeLang/SimpleTerrain ~/SimpleTerrain
To use it from your program:
For example:
import simpleterrain.*;
exported func main() {
my_terrain = MakeSimpleTerrain(1337, 40, 16); 2
my_terrain.display();
}
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
########################################
There's a special module, stdlib, which is automatically added to every build command as if we added stdlib=~/Vale/stdlib/src. It contains a lot of common tools such as lists, hash maps, and functions like sqrt.
This is like Java projects or C++ translation units.
Note how the module name, simpleterrain, doesn't need to match the folder name, SimpleTerrain. You can use anything you want for the module name.
If you're curious: this makes a 40x16 with a random seed of 1337. If you change the 1337 to another number, you'll get different 40x16 terrain!
Some modules (such as the standard library) are very large so they've been divided into several directories, also known as "namespaces".
The standard library has a List class in the stdlib/src/collections/list directory. We need to import each namespace specifically, so to use a List we would type import stdlib.collections.list.*;. 3 at the top of the file that wants to use List, like:
import stdlib.collections.list.*;
exported func main() {
myList = List<int>().add(4).add(2);
foreach i in myList {
print(i);
}
}
42
You can do this for own module too! For example, if you had this:
Then foo.vale could say import mymodule.bar.*; to use the code from baz.vale.
Currently, we're only able to import entire namespaces at a time, hence the .* at the end. In the future, we'll be able to import specific items.
Next: Structs