Modules
Split Soplang programs into files and import definitions with keen.
Modules in Soplang
Split larger programs into multiple .sop files. keen imports another file
and makes its functions, variables, and classes available to the importing program.
Importing a File – keen
Write keen followed by a quoted file path:
utils/xisaab.sop
madoor PI = 3.14159 hawl labajibbaar(x) { celi x * x } qaab Goob { hawl dhis(r) { nafta.r = r } hawl bed() { celi PI * labajibbaar(nafta.r) } }
main.sop
keen "utils/xisaab.sop" qor(labajibbaar(4)) // 16 qor(PI) // 3.14159 door g = cusub Goob(2) qor(g.bed()) // 12.56636
soplang main.sopHow keen Works
- Relative paths are resolved from the directory of the file containing
keen, rather than the terminal's working directory. Imports therefore work whether you runsoplang main.soporsoplang project/main.sop. - Top-level definitions, including functions, variables, constants, and classes, become available in the importing program.
- Top-level code executes when the file is imported, including calls such as
qor(...). - Each file is imported once, including when it is referenced more than once or participates in circular imports.
- A missing file produces an import diagnostic in Somali:
Khalad soo-dejin: Faylka 'nope.sop' ma helin [E021] safka 1, tiirka 1
--> main.sop:1:1
1 │ keen "nope.sop"
│ ^^^^
└─ Hubi in magaca faylka iyo jidka ay sax yihiin.Tips
- Put
keenstatements at the beginning of a file so its dependencies are easy to find. - Give helper files descriptive names, such as
utils/xisaab.sopor_math_lib.sop. - Avoid defining the same name in different imported files. Colliding names can produce a redeclaration error.