Functions and callable values
ASPL 0.7.0 · Current release
Declare typed helpers and store, pass, return, compare and invoke callable values.
On this page
Top-level functions
ASPL 0.7.0 supports these rules in native execution and the browser. Define helpers at file scope with fn / دالة, typed parameters and a typed result. Names are unique within a source module; overloading, nested definitions, closures, default arguments and variadic user functions are unsupported. A declaration cannot replace a builtin alias. Executable statements belong inside functions.
Definitions are order-independent and may recurse. Calls evaluate arguments left to right and pass values by value. Parameters are immutable. Lists and records keep their copy semantics across calls. Every path in a non-unit function must return the declared result type. unit represents no value to store or pass.
Illustrative fragment
fn apply(action: fn(i64) -> i64, value: i64) -> i64 {
return action(value);
}Typed callable values
A named top-level user function is also a value. fn(i64) -> i64 / دالة(ص64) -> ص64 describes one parameter and a result; parameter names are absent from the type. fn() -> unit describes a callable with no parameters and no result. The full parameter and result types must match; assignment does not convert a callable's signature.
Copy, pass, return and invoke a callable, or store it in a list, record or result. Postfix calls may follow a field, list index or returned callable, as in carrier::callback(38), saved[0](38) or choose()(38). No local environment is captured. matches / يطابق and not matches / لا يطابق compare function identity for compatible callable types; ordering and arithmetic are unsupported.
Execution: Browser and native · ASPL 0.7.0
- Uses functions, lists, records and results. No data files are required.
Input: No input lines are required.
fn increment(value: i64) -> i64 { return value + 4; }
fn choose() -> fn(i64) -> i64 { return increment; }
record Carrier { callback: fn(i64) -> i64 }
fn apply(action: fn(i64) -> i64, value: i64) -> i64 { return action(value); }
fn start_main() -> i64 {
let saved: list<fn(i64) -> i64> = [choose()];
let wrapped: result<fn(i64) -> i64> = success(saved[0]);
let carrier = Carrier { callback: wrapped::value };
if carrier::callback not matches increment { return 1; }
print(format_i64(apply(carrier::callback, 38)) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "42\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "42\n"
}
}Accessibility and entry restrictions
Callable values refer only to user functions. The reserved entry start_main / ابدأ_الرئيسية, builtins, foreign declarations and supplied-library functions are not callable values. The entry cannot be called from source either.
Native source-module declarations are private by default. A direct lib::hidden reference fails with private-member. An exported lib::choose() may return a callable to its private helper; receiving and invoking that value is valid. Shared signatures must not expose a private record, including inside a callable, list or result. That failure is private-type-exposure.
Read source modules and exports for native accessibility. The browser supports a single source file and cannot load user source modules.
Failures and complete examples
Wrong arity produces argument-count-mismatch; an incompatible argument, return or callable type produces type-mismatch. A missing non-unit return path produces missing-return. Taking the entry as a value produces undefined-name. These are checking errors, separate from arithmetic or recursion-limit traps during execution.
The factorial helper example returns process status 120 with no output. That is a successful language execution with a nonzero process status, so a shell treats the command as unsuccessful. The callable example prints 42 and returns 0.
Execution: Browser and native · ASPL 0.7.0
- No prior knowledge is required to open and check it.
Input: No input lines are required.
fn factorial(value: i64) -> i64 {
if value <= 1 {
return 1;
}
return value * factorial(value - 1);
}
fn start_main() -> i64 {
return factorial(5);
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "",
"processStatus": 120
},
"native": {
"check": 0,
"status": 120,
"output": ""
}
}See all function variants, callable variants and native check/run/build.