From a0aa4802d1d35b7ad312e930bff211ff581ab94a Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Fri, 15 Nov 2024 11:36:38 -0500 Subject: [PATCH] Add docs for modules and import statements (#4435) --- docs/kcl/modules.md | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 docs/kcl/modules.md diff --git a/docs/kcl/modules.md b/docs/kcl/modules.md new file mode 100644 index 000000000..7d49dc963 --- /dev/null +++ b/docs/kcl/modules.md @@ -0,0 +1,59 @@ +--- +title: "KCL Modules" +excerpt: "Documentation of modules for the KCL language for the Zoo Modeling App." +layout: manual +--- + +`KCL` allows splitting code up into multiple files. Each file is somewhat +isolated from other files as a separate module. + +When you define a function, you can use `export` before it to make it available +to other modules. + +``` +// util.kcl +export fn increment = (x) => { + return x + 1 +} +``` + +Other files in the project can now import functions that have been exported. +This makes them available to use in another file. + +``` +// main.kcl +import increment from "util.kcl" + +answer = increment(41) +``` + +Imported files _must_ be in the same project so that units are uniform across +modules. This means that it must be in the same directory. + +Import statements must be at the top-level of a file. It is not allowed to have +an `import` statement inside a function or in the body of an if-else. + +Multiple functions can be exported in a file. + +``` +// util.kcl +export fn increment = (x) => { + return x + 1 +} + +export fn decrement = (x) => { + return x - 1 +} +``` + +When importing, you can import multiple functions at once. + +``` +import increment, decrement from "util.kcl" +``` + +Imported symbols can be renamed for convenience or to avoid name collisions. + +``` +import increment as inc, decrement as dec from "util.kcl" +```