Files
Toy/tests/scripts/values/test_functions.toy
T
2026-05-30 10:53:21 +10:00

55 lines
791 B
Plaintext

//define and invoke a function
{
fn greeting() {
return "Hello world";
}
assert greeting() == "Hello world", "Function definition failed";
}
//functions within other function argument lists
{
fn outer(arg: String) {
return arg == "Hello world";
}
fn inner() {
return "Hello world";
}
assert outer(inner()), "Functions within argument lists failed";
}
//first class functions
{
fn hello() {
print "Hello world";
}
fn identity(x) {
return x;
}
assert identity(hello) == hello, "First class functions failed";
}
//closures
{
//closures
fn makeCounter() {
var counter: Int = 0;
fn increment() {
return ++counter;
}
return increment;
}
var tally: Function = makeCounter();
tally();
tally();
tally();
assert tally() == 4, "closures failed";
}