Vale can call functions written in other languages, such as C. These "external" functions are known as extern functions.
Here, main is calling out into an external C function that reads an int from the keyboard.
The fn readInt() int extern; is telling Vale that the C code will declare a function named readInt.
The C code must declare the function with extern, so it's visible to Vale.
The above Vale code called into a C function. The opposite is also possible, C code can call into Vale code. These Vale functions are known as export functions.
Here, a Vale main is calling into a C cFunc function, which is calling into a Vale exported triple function.
Vale automatically provides a header file so C can call the function.
Recall that a struct can either be an instance or shared. The default is instance. These offer different benefits when calling into (or from) outside code.
When we send an immutable struct from Vale into C, we're actually sending a copy. 0
Here, a Vale main function is sending an immutable Mat3 struct into C.
The C code should include "Vec3.h", which is generated by the Vale compiler.
We can give C a mutable struct too.
C will receive an "opaque handle", something that points to the struct, but C cannot read its members. It must ask a Vale function to read any members.
The benefit of using mutable structs in externs is that Vale doesn't have to copy the entire thing when we give it to C.
Here, a Vale main function is giving C a reference to a Ship.
The C function halfFuel is then asking the Vale function getFuel what the contained fuel is, so C can do its calculation, and return the result.
Instance structs do not incur this cost. More on that below!