fpp/docs/users-guide/Defining-Modules.adoc
2024-06-25 13:01:46 -07:00

105 lines
2.3 KiB
Plaintext

== Defining Modules
In an FPP model, a *module* is a group of model elements that are all qualified
with a name, called the *module name*.
An FPP module corresponds to a namespace in {cpp} and a module in Python.
Modules are useful for (1) organizing a large model into a hierarchy of smaller
units and (2) avoiding
<<Defining-Constants_Names_Name-Clashes,name clashes>>
between different units.
To define a module, you write the keyword `module` followed by one
or more definitions enclosed in curly braces.
For example:
[source,fpp]
----
module M {
constant a = 1
}
----
The name of a module qualifies the names of all the definitions that the module
encloses.
To write the qualified name, you write the qualifier, a dot, and the base name:
for example `M.a`. (This is also the way that
name qualification works in Python, Java, and Scala.)
Inside the module, you can use the qualified name or the unqualified
name.
Outside the module, you must use the qualified name.
For example:
[source,fpp]
--------
module M {
constant a = 1
constant b = a # OK: refers to M.a
constant c = M.b
}
constant a = M.a
constant c = b # Error: b is not in scope here
--------
As with namespaces in {cpp}, you can close a module definition and
reopen it later.
All the definitions enclosed by the same name go in the module
with that name.
For example, the following code is allowed:
[source,fpp]
----
module M {
constant a = 0
}
module M {
constant b = 1
}
----
It is equivalent to this code:
[source,fpp]
----
module M {
constant a = 0
constant b = 1
}
----
You can define modules inside other modules.
When you do that, the name qualification works in the obvious way.
For example:
[source,fpp]
----
module A {
module B {
constant c = 0
}
}
constant c = A.B.c
----
The inside of a module definition is an
<<Defining-Constants_Multiple-Definitions-and-Element-Sequences,element sequence>>
with a semicolon as the optional terminating punctuation.
For example, you can write this:
[source,fpp]
----
module M { constant a = 0; constant b = 1 }; constant c = M.a
----
A module definition is an
<<Writing-Comments-and-Annotations_Annotations,annotatable element>>,
so you can attach annotations to it, like this:
[source,fpp]
----
@ This is module M
module M {
constant a = 0
}
----