[SOLVED] Module Declaration Order

Just starting out? Need help? Post your questions and find answers here.
User avatar
TI-994A
Addict
Addict
Posts: 2754
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

[SOLVED] Module Declaration Order

Post 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. :?
Last edited by TI-994A on Sun Oct 26, 2025 3:18 pm, edited 1 time in total.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Little John
Addict
Addict
Posts: 4807
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Module Declaration Order

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2754
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Module Declaration Order

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [SOLVED] Module Declaration Order

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
TI-994A
Addict
Addict
Posts: 2754
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: [SOLVED] Module Declaration Order

Post 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:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply