Modules
A module keeps a group of names to itself. Its variables, constants, structures and procedures can't clash with the program's or another module's, so two modules can both have a Report() or a count, and a library can use any names it likes inside.
A module has two parts:
DeclareModule Name
; the public part: what the rest of the program can use
EndDeclareModule
Module Name
; the implementation: procedure bodies and private names
EndModule
DeclareModule and Module ✅
Everything declared in DeclareModule is public: constants, structures, Global variables, Declared procedures, prototypes. Everything declared only in Module is private. Outside the module, a public name is written Name::thing. That also works for Name::#Constant, Name::*pointer and types, as in Define p.Name::Pair.
OpenConsole()
DeclareModule Shapes
#Sides = 4
Structure Box
w.i
h.i
EndStructure
Global made
Declare.i Area(*b.Box)
EndDeclareModule
Module Shapes
Procedure.i Area(*b.Box)
made + 1
ProcedureReturn *b\w * *b\h
EndProcedure
EndModule
Define b.Shapes::Box
b\w = 3 : b\h = 5
PrintN(Str(Shapes::Area(@b)) + " " + Str(Shapes::#Sides) + " " + Str(Shapes::made))
PrintN(Str(SizeOf(Shapes::Box)))
Output:
15 4 1
16
DeclareModule must come before Module, and modules can't be nested or declared inside a procedure. Code at the top level of a module runs where the module is in the source, like the program's own top-level code.
Separate names ✅
Inside a module, only these names can be used without Name:::
- the module's own names, public and private;
- the public names of modules brought in with
UseModule; - everything built in: commands,
#PB_constants, built-in structures.
The program's own globals, constants and procedures can't be seen in a module. A name a module uses without declaring is the module's own, even if the program has the same name. In the example below, count and Report() exist three times without clashing.
OpenConsole()
Global count = 100
Procedure.s Report()
ProcedureReturn "main " + Str(count)
EndProcedure
DeclareModule A
Global count
Declare.s Report()
EndDeclareModule
Module A
Global hidden = 5 ; private: A::hidden is an error
count = 1
Procedure.s Report()
ProcedureReturn "A " + Str(count + hidden)
EndProcedure
EndModule
DeclareModule B
Declare.s Report()
EndDeclareModule
Module B
count = 2 ; B's own, not the program's
Procedure.s Report()
ProcedureReturn "B"
EndProcedure
EndModule
PrintN(Report() + ", " + A::Report() + ", " + B::Report() + ", " + Str(A::count))
Output:
main 100, A 6, B, 1
Using a private name from outside is a compile error: 'hidden' is not public in module 'a' (declare it in DeclareModule).
UseModule and UnuseModule ✅
UseModule Name makes a module's public names usable without Name::, from that line on. UnuseModule Name takes them away again. Inside a module, only that module's own UseModule lines count. One in the program doesn't reach into a module, and one in DeclareModule doesn't reach into Module, so put it where the names are used.
OpenConsole()
DeclareModule Greet
#Mark$ = "!"
Declare.s Hello(name$)
EndDeclareModule
Module Greet
Procedure.s Hello(name$)
ProcedureReturn "Hello, " + name$ + #Mark$
EndProcedure
EndModule
DeclareModule Loud
Declare.s Shout(name$)
EndDeclareModule
Module Loud
UseModule Greet ; Loud uses Greet's names
Procedure.s Shout(name$)
ProcedureReturn UCase(Hello(name$))
EndProcedure
EndModule
UseModule Greet
PrintN(Hello("x") + " " + #Mark$)
UnuseModule Greet
PrintN(Loud::Shout("modules"))
Output:
Hello, x! !
HELLO, MODULES!
When the program declares a name itself, its own one wins over a used module's. When two used modules both have a name, the one used first wins. PureBasic reports both of these as errors, so it's best not to rely on them.
A public structure, interface or class's name works after UseModule wherever a type is written, p.Point = q included.
Classes can be declared in modules too: see Classes in modules.
Macros, labels and Defined ✅
A macro belongs to the module that defines it, like the module's other names:
- Inside the module, its own macro wins over any macro of the same name outside.
- A macro in
DeclareModuleis public:Name::Macroworks anywhere, and so doesMacroafterUseModule Name. In the program, a macro of its own with the same name wins. - A macro in
Moduleis private:Name::Macrofrom outside is an error. - The program's own macros are not visible inside a module.
UndefineMacroinside a module removes that module's macro.
A macro's body is read again where it's used, so a public macro whose body uses the module's other names should write them as Name::thing.
Labels belong to their module too. Two modules can each have a DataSection label Table:, and Restore Table, Goto, Gosub and ?Table inside each module find that module's label.
Defined(Name, #PB_Module) is 1 once the module has appeared in the source above it, so it works as an include guard: CompilerIf Not Defined(Shapes, #PB_Module) around a module that may be included twice.
OpenConsole()
PrintN("before: " + Str(Defined(Shapes, #PB_Module)))
DeclareModule Shapes
Macro Square(x) : ((x) * (x)) : EndMacro ; public
Declare.i Sides(n)
EndDeclareModule
Module Shapes
Macro Pick(i) : (i) : EndMacro ; private
Procedure.i Sides(n)
Restore Table
For i = 1 To Pick(n)
Read.i s
Next
ProcedureReturn s
EndProcedure
DataSection
Table:
Data.i 3, 4, 5
EndDataSection
EndModule
Macro Pick(i) : (i * 100) : EndMacro ; the program's own
DataSection
Table: ; no clash with Shapes' Table
Data.i 42
EndDataSection
Restore Table
Read.i first
PrintN(Str(Shapes::Square(3)) + " " + Str(Shapes::Sides(2)) + " " + Str(Pick(1)) + " " + Str(first))
PrintN("after: " + Str(Defined(Shapes, #PB_Module)))
Output:
before: 0
9 4 100 42
after: 1
Not done yet
Name :: thingwith spaces around::is not accepted. Write it without spaces.