Functions
Stof functions.
In Stof, functions are first-class data. You can:
Store them in variables
Pass them around as values
Send them over APIs
Execute them anywhere
Coffee: {
const bool decaf: false // never!!
bool light_roast: false
Customer: {
name: "Stanly Yelnatz III"
time: Time.now()
}
Shop: {
str name: "Coffee Pot"
str address: "42 Wallaby Way, Sydney"
fn order() -> str {
const order = `${self.name} on ${self.address}\n`;
order.push(`1 ${super.light_roast ? 'light' : 'dark'} roast Coffee`);
order
}
}
fn customer_name() -> str {
self.Customer.name
}
}
#[main]
fn main() -> void {
pln(self.Coffee.customer_name());
pln(self.Coffee.Shop.order());
}Async
The Stof runtime supports async processes at its core. Functions can be async with an #[async] attribute (or the async syntax that adds this attribute).
Flexible Async
Because async is so foundational in Stof, any function, even ones not marked as async can be called asynchronously.
Attributes
Just like fields, functions can have custom attributes.
Arrow Functions
As a Field
There are times when functions (or function pointers "fn") end up as a field. You can call them just like normal functions!
Return
A return statement is one without a semi-colon, returning the last value on the stack. Or, an explicit "return" statement within the function.
Static Functions
Stof doesn't have a static concept like other languages, but it does have prototypes. And because prototypes have a type name, you can call functions directly on that prototype using the name instead of the object, mimicking behavior that would seem like a "static" function in another language.
Pro tip: I like to put a #[static] attribute on the functions that I intend to call this way, so that it's obviously separated from the type interface, even though this attribute is not inherently special.
Last updated