You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A module function is a function that is accessible to loaded modules. All external functions are module functions. Loaded functions can be promoted to a module function with the mod keyword.
For example, in a loader script:
mod math() = load("src/math.dyon")
fn main(){
main := unwrap(load("src/main.dyon"))
call(main,"main",[])}
A module function can be used for importing functions into scope:
use math()::{
mat4_add asadd,
mat4_mul asmul}fnmain(){
...a:= add(b, c)
...
}
Import from source
Since the "load" intrinsic is a module function, it can be used directly to import functions:
use load("src/math.dyon")::{
mat4_add asadd,
mat4_mul asmul}fnmain(){
...a:= add(b, c)
...
}
This trick can also be used with:
Package managers
Parser generators
Configuration and setup scripts
Dependent imports
Imports are called one by one when loading and checked for imported functions. Since the functions are added to the prelude, it can be used to send values to the next import:
use load("src/config.dyon")::{settings}use physics(settings())::{update, collide}
This can be used to optimize code in advance, tailored to specific usage.
A more advanced example:
// Import `foo` into prelude of "bar.dyon".use load(source:"src/bar.dyon", imports:[unwrap(foo())])::{bar_a, bar_b}
Comments
This is not that different from how things are done currently through loading scripts. The difference is:
better control over single functions
rename functions for less typing or to avoid naming collision
simplifies loading scripts
make loaded modules "inherit" some functions using the mod keyword
loaded modules can choose which modules they need, which is nice when there is a large number of modules available and you only need a few of them
The text was updated successfully, but these errors were encountered:
Alternative to #391.
A module function is a function that is accessible to loaded modules. All external functions are module functions. Loaded functions can be promoted to a module function with the
mod
keyword.For example, in a loader script:
A module function can be used for importing functions into scope:
Import from source
Since the "load" intrinsic is a module function, it can be used directly to import functions:
This trick can also be used with:
Dependent imports
Imports are called one by one when loading and checked for imported functions. Since the functions are added to the prelude, it can be used to send values to the next import:
This can be used to optimize code in advance, tailored to specific usage.
A more advanced example:
Comments
This is not that different from how things are done currently through loading scripts. The difference is:
mod
keywordThe text was updated successfully, but these errors were encountered: