Code structure problem with Modules

Just starting out? Need help? Post your questions and find answers here.
Barbarossa
User
User
Posts: 47
Joined: Fri Oct 12, 2012 8:50 am

Code structure problem with Modules

Post by Barbarossa »

Hi all,

I am trying to restructure my code into modules. But I am struggling with the "global" structures.

Let's consider the following code:

Code: Select all

DeclareModule Mod1
	
	Structure CommonStructure
		Number.i
		String.s
	EndStructure
	
	Declare Mod1_Function(*par.CommonStructure)

EndDeclareModule

Module Mod1
	
	Procedure Mod1_Function(*par.CommonStructure)
		Debug *par\Number
	EndProcedure
	
EndModule

DeclareModule Mod2
	
	Structure CommonStructure
		Number.i
		String.s
	EndStructure
	
	Declare Mod2_Function(*par.CommonStructure)

EndDeclareModule

Module Mod2
	
	Procedure Mod2_Function(*par.CommonStructure)
		Debug *par\String
	EndProcedure
	
EndModule

; Main Scope

Structure CommonStructure
	Number.i
	String.s
EndStructure

NewList Test.CommonStructure()	
AddElement(Test())
Test()\Number=1
Test()\String="Test"

UseModule Mod1   ; this raises an error because the compiler inserts Commonstructure 
UseModule Mod2	; into the main scope in which it already exists

Mod1_Function(@Test())
Mod2_Function(@Test())

End
My question is, how can I best solve this?

I can think of two solutions. Both kinda defeat the purpose of using modules. They are:
1. Give every structure an unique name:
- Main_CommonStructure
- Mod1_CommonStructure
- Mod2_CommonStructure
Not really what you would want. Difficult to update the structures too.
2. Don't use UseModule and use prefix Mod1::Mod1_Function() instead
Now you lose the autocomplete and it gives you also longer commands,

Maybe there is a better way. Any input is appreciated!

John
Little John
Addict
Addict
Posts: 4778
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Code structure problem with Modules

Post by Little John »

Barbarossa wrote:2. Don't use UseModule and use prefix Mod1::Mod1_Function() instead
That's what I always do.
AFAIK it's the only safe way to avoid the naming conflicts which you mentioned.

BTW: Writing something like Mod1_ is not necessary then. It's sufficient to use the respective module prefixes:

Code: Select all

Mod1::MyFunction()
Mod2::MyFunction()
Mod3::MyFunction()
User avatar
mk-soft
Always Here
Always Here
Posts: 6208
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Code structure problem with Modules

Post by mk-soft »

I use it a global module

Code: Select all

DeclareModule ModCommon
  Structure CommonStructure
    Number.i
    String.s
  EndStructure
EndDeclareModule

Module ModCommon
  ; Global functions
EndModule

DeclareModule Mod1
  
  Declare Mod1_Function(*par)
  
EndDeclareModule

Module Mod1
  
  Structure CommonStructure
    Number.i
    String.s
  EndStructure

  Procedure Mod1_Function(*par.CommonStructure)
    Debug *par\Number
  EndProcedure
  
EndModule

DeclareModule Mod2
  
  Declare Mod2_Function(*par)
  
EndDeclareModule

Module Mod2
  
  Structure CommonStructure
    Number.i
    String.s
  EndStructure

  Procedure Mod2_Function(*par.CommonStructure)
    Debug *par\String
  EndProcedure
  
EndModule

; Main Scope

UseModule ModCommon
NewList Test.CommonStructure()   
AddElement(Test())
Test()\Number=1
Test()\String="Test"

UseModule Mod1   ; this raises an error because the compiler inserts Commonstructure 
UseModule Mod2   ; into the main scope in which it already exists

Mod1_Function(@Test())
Mod2_Function(@Test())

End
:wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Barbarossa
User
User
Posts: 47
Joined: Fri Oct 12, 2012 8:50 am

Re: Code structure problem with Modules

Post by Barbarossa »

Thanks for the answers. I think I'll go with LittleJohns Solution for now.
MK-softs solution doesn't really work and gives me the same problems as before.

I really think the modules need some more improvement for them to work like they should in theory.

John
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Code structure problem with Modules

Post by Fred »

IMHO, your should use a global module, and import it in your submodule, so you will actually share the structure:

Code: Select all

DeclareModule ModCommon
  Structure CommonStructure
    Number.i
    String.s
  EndStructure
EndDeclareModule

Module ModCommon
  ; Global functions
EndModule


DeclareModule Mod1
  Declare Function(*par)
EndDeclareModule

Module Mod1
  UseModule ModCommon

  Procedure Function(*par.CommonStructure)
    Debug *par\Number
  EndProcedure
 
EndModule


DeclareModule Mod2
  Declare Function(*par)
EndDeclareModule

Module Mod2
  UseModule ModCommon

  Procedure Function(*par.CommonStructure)
    Debug *par\String
  EndProcedure
EndModule

; Main Scope

UseModule ModCommon

NewList Test.CommonStructure()   
AddElement(Test())
Test()\Number=1
Test()\String="Test"

Mod1::Function(@Test())
Mod2::Function(@Test())

End
User avatar
mk-soft
Always Here
Always Here
Posts: 6208
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Code structure problem with Modules

Post by mk-soft »

On this idea I did not come :D
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Code structure problem with Modules

Post by Blue »

@Fred

(1) What would be the best place to insert EnableExplicit so that it governs the entirerity of the example you provided above ?
(2) As a general rule, should EnableExplicit appear in the Module part or the DeclareModule part ?
Last edited by Blue on Fri Sep 25, 2015 5:55 pm, edited 1 time in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Code structure problem with Modules

Post by Fred »

At the top of the file and inside each DeclareModule
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Code structure problem with Modules

Post by Blue »

Oh la la !! Rapide le monsieur que je pensais trop occupé à pourchasser les bogues pour s'occuper du vrai monde... :shock:

Thank you Fred
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Post Reply