Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module functions and dependent parameterized imports #393

Closed
bvssvni opened this issue Oct 2, 2016 · 1 comment
Closed

Module functions and dependent parameterized imports #393

bvssvni opened this issue Oct 2, 2016 · 1 comment

Comments

@bvssvni
Copy link
Member

bvssvni commented Oct 2, 2016

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:

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 as add,
    mat4_mul as mul
}

fn main() {
    ...
    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 as add,
    mat4_mul as mul
}

fn main() {
    ...
    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
@bvssvni
Copy link
Member Author

bvssvni commented Mar 5, 2017

Closed in favor of optional namespaces.

@bvssvni bvssvni closed this as completed Mar 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant