Repository navigation
Namespace
Home › Modularizing › Namespace
Declares a namespace block.
Namespace identifier [ Alias "aliasname" ]
statements
End Namespaceidentifieraliasname
Namespaces allow to group entities like objects (predefined data-types and UDTs including Union and Enum) and procedures (including their declarations) under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
Whether or not explicitly declared a namespace in a source file, the compiler adds a default namespace. This unnamed namespace, called the global namespace, is present in every file.
Any identifier in the global namespace is available for use in a named namespace (even global symbols with the same name as keywords may be declared inside a namespace).
Namespaces implicitly have public access and this is not modifiable.
A variable declared inside a namespace is always implicitly static and visible throughout the entire program even if the declaration modifier Shared is not specified (static and shared are optional, but this may improve code readability).
Namespaces do not have any effect on the visibility of a define.
It is possible to define a namespace in two or more declarations.
Namespaces are commonly used in libraries where you don't want all the symbols from that library to crowd the user's space (called the global namespace).
For example, if you used the "Forms" library, it might define the Point type for describing an X and Y coordinate, and you might also define it for another purpose. This can be resolved by creating the namespace Forms for the library, and then referring to its Point type as Forms.Point, and yours as just Point.
To access from outside a defined symbol in a namespace, add the namespace identifier followed by a dot as a prefix of the symbol, or bring the namespace symbols into the current scope by means of the Using (Namespaces) statement.
To access duplicated symbols defined as global outside the namespace, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SomeSymbol (or only ..SomeSymbol if inside a With..End With block).
Note: The parser allows to define anonymous Namespaces (without identifier term), but this is the only similarity with the actual C++ capability: The FB compiler automatically generates multiple separate anonymous Namespaces instead of one only per module in such a case.
The FB anonymous Namespaces are almost unusable because all their declarations are inaccessible, even from the body of the module that contains them. Apart from encapsulating module constructors/destructors also inside, nothing else can be done with them.
- Namespaces are not supported in the -lang qb dialect.
- New to FreeBASIC
Namespace Forms
Type Point '' A 2D point
As Integer x
As Integer y
End Type
'' Since we are inside of the namespace, Point resolves to Forms.Point.
Sub AdjustPoint( ByRef pt As Point, ByVal newx As Integer, ByVal newy As Integer )
pt.x = newx
pt.y = newy
End Sub
End Namespace
Type Point '' A 3D point
As Integer x
As Integer y
As Integer z
End Type
Sub AdjustPoint( ByRef pt As Point, ByVal newx As Integer, ByVal newy As Integer, ByVal newz As Integer )
pt.x = newx
pt.y = newy
pt.z = newz
End Sub
Dim pt1 As Point
AdjustPoint( pt1, 1, 1, 1 )
Dim pt2 As Forms.Point
Forms.AdjustPoint( pt2, 1, 1 )Namespaces are GCC C++ compatible, the following code aims to test that.
(cpp)
// mylib.cpp
// To compile:
// g++ -c mylib.cpp -o mylib.o
// ar rcs libmylib.a mylib.o
#include <string.h>
#include <ctype.h>
namespace mylib
{
int test()
{
return 123;
}
}'' test.bas
Extern "c++" Lib "mylib"
Namespace mylib Alias "mylib"
Declare Function test() As Integer
End Namespace
End Extern
Print mylib.test()Category: Modularizing
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing