Page 1 of 1

[SOLVED] Module Declaration Order

Posted: Sun Oct 26, 2025 2:38 pm
by TI-994A
Hi everyone! I've been working on a project that employs modules, and I seem to have stumbled upon a roadblock.

How should functions in mod2 be declared so that it could be called from mod1?

Code: Select all

; module 1

DeclareModule mod1
  Declare add(a, b)  
  Declare modAdd(a, b)
EndDeclareModule

Module mod1        
  Procedure add(a, b)
    ProcedureReturn a + b
  EndProcedure  
  
  Procedure modAdd(a, b)
    ;ProcedureReturn mod2::add(a, b)
  EndProcedure  
EndModule


; module 2

DeclareModule mod2
  Declare add(a, b)
  Declare modAdd(a, b)
EndDeclareModule

Module mod2
  Procedure add(a, b)
    ProcedureReturn a + b
  EndProcedure  
  
  Procedure modAdd(a, b)
    ProcedureReturn mod1::add(a, b)
  EndProcedure    
EndModule


; main program

OpenWindow(0, 0, 0, 300, 200, "Module Declatations", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0, 0, 0, 300, 200)
AddGadgetItem(0, -1, Str(mod1::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod2::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod1::modAdd(1, 2)))  ; error > Module not found: mod2
AddGadgetItem(0, -1, Str(mod2::modAdd(1, 2)))  ; works!

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

I feel I must have missed something. :?

Re: Module Declaration Order

Posted: Sun Oct 26, 2025 2:44 pm
by Little John

Code: Select all

DeclareModule mod1
   Declare add(a, b)  
   Declare modAdd(a, b)
EndDeclareModule

DeclareModule mod2
   Declare add(a, b)
   Declare modAdd(a, b)
EndDeclareModule


Module mod1        
   Procedure add(a, b)
      ProcedureReturn a + b
   EndProcedure  
   
   Procedure modAdd(a, b)
      ProcedureReturn mod2::add(a, b)
   EndProcedure  
EndModule


Module mod2
   Procedure add(a, b)
      ProcedureReturn a + b
   EndProcedure  
   
   Procedure modAdd(a, b)
      ProcedureReturn mod1::add(a, b)
   EndProcedure    
EndModule


; main program

OpenWindow(0, 0, 0, 300, 200, "Module Declatations", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0, 0, 0, 300, 200)
AddGadgetItem(0, -1, Str(mod1::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod2::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod1::modAdd(1, 2)))  ; => works!
AddGadgetItem(0, -1, Str(mod2::modAdd(1, 2)))  ; works!

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

Re: Module Declaration Order

Posted: Sun Oct 26, 2025 3:17 pm
by TI-994A
Little John wrote: Sun Oct 26, 2025 2:44 pm

Code: Select all

DeclareModule mod1
   Declare add(a, b)  
   Declare modAdd(a, b)
EndDeclareModule

DeclareModule mod2
   Declare add(a, b)
   Declare modAdd(a, b)
EndDeclareModule


Module mod1        
   Procedure add(a, b)
      ProcedureReturn a + b
   EndProcedure  
   
   Procedure modAdd(a, b)
      ProcedureReturn mod2::add(a, b)
   EndProcedure  
EndModule


Module mod2
   Procedure add(a, b)
      ProcedureReturn a + b
   EndProcedure  
   
   Procedure modAdd(a, b)
      ProcedureReturn mod1::add(a, b)
   EndProcedure    
EndModule


; main program

OpenWindow(0, 0, 0, 300, 200, "Module Declatations", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0, 0, 0, 300, 200)
AddGadgetItem(0, -1, Str(mod1::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod2::add(1, 2)))     ; works!
AddGadgetItem(0, -1, Str(mod1::modAdd(1, 2)))  ; => works!
AddGadgetItem(0, -1, Str(mod2::modAdd(1, 2)))  ; works!

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Oh, wow! What a doofus thing to miss. :oops:

It's so obvious that it's not! :lol:

I have always placed the modules and their declarations into the same include files. Clearly the wrong approach.

Thank you, LJ! Truly appreciated.

Re: [SOLVED] Module Declaration Order

Posted: Sun Oct 26, 2025 4:39 pm
by skywalk
Sorry, I still don't see the benefit?

mod1_add(), etc in mod1.pbi
And
mod2_add(), etc in mod2.pbi

I never have to remember order of declares or when to use ::?
And I can use code tools with very simple PB syntax checks.

Rant off.

Re: [SOLVED] Module Declaration Order

Posted: Mon Oct 27, 2025 6:48 pm
by TI-994A
skywalk wrote: Sun Oct 26, 2025 4:39 pmSorry, I still don't see the benefit?
Hi @skywalk. You're absolute right. There is no need to fuss with modules and their added protocols, as the same could be achieved with included files and some creative naming conventions.

But with modules being in separate namespaces, there is an inherent freedom of implementing common names for procedures, gadgets, variables, etc, without the risk of clashing with the global scope or that of other included environments. They are then simply called with the module prefix, just as it would be done in the conventional way, but without the fuss of hardcoding the prefixes to all the labels. Moreover, if referenced within UseModule/UnuseModule blocks, the individual prefixes can be omitted altogether.

mod1.pbi

Code: Select all

DeclareModule mod1
  Enumeration
    #input
    #output
  EndEnumeration  
  
  Declare add(a, b)   
  Define window, canvas, button  
EndDeclareModule

Module mod1    
  EnableExplicit
  
  Procedure add(a, b)
    ProcedureReturn a + b  
  EndProcedure
  
  window = OpenWindow(#PB_Any, 0, 0, 300, 200, "Mod1 Window")
  canvas = CanvasGadget(#PB_Any, 0, 0, 300, 200, #PB_Canvas_Container)
  button = ButtonGadget(#PB_Any, 10, 150, 100, 30, "Button")
  
  CompilerIf #PB_Compiler_IsMainFile
    While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
  CompilerEndIf
  
EndModule

include1.pbi

Code: Select all

Enumeration
  #inc1_input
  #inc1_output
EndEnumeration

Define inc1_window, inc1_canvas, inc1_button

Procedure inc1_add(a, b)
  ProcedureReturn a + b  
EndProcedure

inc1_window = OpenWindow(#PB_Any, 0, 0, 300, 200, "Include1 Window")
inc1_canvas = CanvasGadget(#PB_Any, 0, 0, 300, 200, #PB_Canvas_Container)
inc1_button = ButtonGadget(#PB_Any, 10, 150, 100, 30, "Button")

CompilerIf #PB_Compiler_IsMainFile
  While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
CompilerEndIf

The elegance and convenience is quite undeniable. IMHO, anyway. :D

skywalk wrote: Sun Oct 26, 2025 4:39 pmI never have to remember order of declares...
Modules or not, when including multiple files with reusable code, we'd still need to keep track of the declaration order. Otherwise the earlier includes won't have visibility of the latter ones. :wink: