Module Global Subs and Functions and Module Vars Using Second Method of Manage Reusable Procedures

General FreeBASIC programming questions.
Post Reply
gplcoder
Posts: 1
Joined: May 10, 2024 17:39
Location: Southwestern Ontario Canada
Contact:

Module Global Subs and Functions and Module Vars Using Second Method of Manage Reusable Procedures

Post by gplcoder »

Hi All,

I am currently porting a large amount of C API's and apps to FreeBASIC and have decided to organize the code as one large file to be compiled by utilizing the second method of 'Manage[ing] Reusable Procedures by Including Source vs Compiled Modules' (https://www.freebasic.net/wiki/ProPgSou ... iledModule). I am trying to implement private subs and functions as well as private var (by private, I mean local to the module code only).

Is there a way of implementing these items with a module only scope when everything is in one file (in memory) during compilation?

TIA
fxm
Moderator
Posts: 12219
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Module Global Subs and Functions and Module Vars Using Second Method of Manage Reusable Procedures

Post by fxm »

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.

Each include module file has its own NAMESPACE scope with a specific name. If a main code body exists in a module file, put it in a SCOPE...END SCOPE block with its other non-static local variables, and use not USING(Namespaces) but the full path to access its NAMESPACE scope.

Purely academic example (with identical symbol names, each in its own scope):
- module1.bi:

Code: Select all

Namespace module1
    Type UDT
        Dim As String s = "module1"
    End Type
    
    Sub printMyName(Byref u As UDT)
        Print u.s
    End Sub
End Namespace

' if main code body exists in module file
Scope
    Dim As module1.UDT u
    module1.printMyName(u)
End Scope
- module2.bi:

Code: Select all

Namespace module2
    Type UDT
        Dim As String s = "module2"
    End Type
    
    Sub printMyName(Byref u As UDT)
        Print u.s
    End Sub
End Namespace

' if main code body exists in module file
Scope
    Dim As module2.UDT u
    module2.printMyName(u)
End Scope
- module.bas:

Code: Select all

#include "module1.bi"
#include "module2.bi"

Print

Dim As module1.UDT u1
module1.printMyName(u1)

Dim As module2.UDT u2
module2.printMyName(u2)

Sleep
Last edited by fxm on Jul 03, 2024 18:31, edited 3 times in total.
Reason: Completed descriptive and added example.
Post Reply