Page 1 of 1

module question

Posted: Sat Jun 15, 2024 10:22 pm
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?

Re: module question

Posted: Sat Jun 15, 2024 11:21 pm
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()

Re: module question

Posted: Sun Jun 16, 2024 1:06 am
by RSrole
Thanks, so I'm doing something wrong.

Re: module question

Posted: Sun Jun 16, 2024 1:48 am
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

Re: module question

Posted: Sun Jun 16, 2024 1:50 am
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

Re: module question

Posted: Sun Jun 16, 2024 7:34 am
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.

Re: module question

Posted: Mon Jun 17, 2024 5:33 am
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