module question

Just starting out? Need help? Post your questions and find answers here.
RSrole
User
User
Posts: 71
Joined: Fri Apr 29, 2022 8:27 pm

module question

Post by RSrole »

Can you call a module from within a module? I tried to do that and got an error "Module not found (and the details)". I can call from the main without problem. Or am I doing something wrong?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: module question

Post by jassing »

RSrole wrote: Sat Jun 15, 2024 10:22 pm Can you call a module from within a module? I tried to do that and got an error "Module not found (and the details)". I can call from the main without problem. Or am I doing something wrong?

Code: Select all

DeclareModule mod1
  Declare func1()
EndDeclareModule
Module mod1
  Procedure func1()
    Debug "mod1::Func1()"
  EndProcedure
EndModule

DeclareModule mod2
  Declare func1()
EndDeclareModule
Module mod2
  Procedure func1()
    mod1::func1()    
  EndProcedure
EndModule

mod2::func1()
RSrole
User
User
Posts: 71
Joined: Fri Apr 29, 2022 8:27 pm

Re: module question

Post by RSrole »

Thanks, so I'm doing something wrong.
RSrole
User
User
Posts: 71
Joined: Fri Apr 29, 2022 8:27 pm

Re: module question

Post by RSrole »

I see, it cares about the order in the source code Mod2 can only call Mod1 because it follows Mod2. Mod1 can't call mod2
r-i-v-e-r
User
User
Posts: 20
Joined: Thu May 09, 2024 5:18 pm

Re: module question

Post by r-i-v-e-r »

RSrole wrote: Sun Jun 16, 2024 1:06 am Thanks, so I'm doing something wrong.
Is the module you're trying to invoke a method from declared prior to the caller module? E.g., in @jassing's example, mod2 may access mod1, but not the other way around due to how they're declared.

If both modules happen to be interdependent (which IMO is less than ideal), you may declare both prior to their implementations, e.g.:

Code: Select all

DeclareModule mod1
  Declare func1()
EndDeclareModule

DeclareModule mod2
  Declare func1()
EndDeclareModule

Module mod1
  Procedure func1()
    Debug "mod1::Func1()"
  EndProcedure
  
  mod2::func1()
EndModule

Module mod2
  Procedure func1()
    mod1::func1()
  EndProcedure
EndModule
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: module question

Post by Little John »

By the way, this problem is not specific to modules, but affects procedures outside of modules as well, because PureBasic is a single-pass compiler.
RSrole
User
User
Posts: 71
Joined: Fri Apr 29, 2022 8:27 pm

Re: module question

Post by RSrole »

Yes this makes sense and thanks. It didn't occur to me to just put the DeclareModules in my Declares.pbi. That does solve it.

Thanks
Post Reply