(Updated Mon 2024-08-26)

Modularity in Fexl

In Fexl, modularity is the ability to share common code across multiple program files. This allows you to define functions in one file and use them in several other files without having to copy the definitions everywhere. You can start writing code in a single file, and refactor it into separate files as needed.

To illustrate how this works, I'll start with an example. Let's say you're writing some code related to the old Flintstones cartoon. You can start by putting everything in one file:

try.fxl:

\\fred=(say "I am Fred.")
\\wilma=(say "I am Wilma.")
\\barney=(say "I am Barney.")
\\betty=(say "I am Betty.")

say "Meet the Flintstones."
fred
wilma
barney
betty

Note that you define the functions with \\ instead of \ because they have side effects and you don't want them evaluated immediately at the point of definition.

If you run that program, you see:

$ fexl try.fxl
Meet the Flintstones.
I am Fred.
I am Wilma.
I am Barney.
I am Betty.

If that's all you need to do, you're done. Just leave it alone.

Moving code to separate files

Now you would like to move those definitions into a separate file. First you create a file called "flintstones.fxl" and define all the functions like this:

flintstones.fxl:

\\fred=(say "I am Fred.")
\\wilma=(say "I am Wilma.")
\\barney=(say "I am Barney.")
\\betty=(say "I am Betty.")

def "fred" fred;
def "wilma" wilma;
def "barney" barney;
def "betty" betty;
std

Then change your main program to this:

try.fxl:

\std=(extend std; use "flintstones.fxl")

extend std
\;
say "Meet the Flintstones."
fred
wilma
barney
betty

Multiple libraries

You can chain together as many libraries as you wish, for example:

\std=(extend std; use "lib1.fxl")
\std=(extend std; use "lib2.fxl")
\std=(extend std; use "lib3.fxl")

extend std
\;
# ... your code here

Each successive library stacks some more definitions on top of the std context, returning a new updated context without altering the original.

Each library file looks roughly like this:

\f1=(...)
\f2=(...)

def "f1" f1;
def "f2" f2;
std